热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

BigDecimal的8种round舍入模式

写段代码,用一个数字(正数或负数),依次使用8种不同的模式,看i参数public static void demo(BigDeci

写段代码,用一个数字(正数或负数),依次使用8种不同的模式,看i参数

public static void demo(BigDecimal bigDecimal, int scale){System.out.println();System.out.print(bigDecimal.toString()&#43;"\t");//循环使用8种舍入模式for (int i &#61;0 ;i<8;i&#43;&#43;){try{System.out.print(bigDecimal.setScale(scale,i)&#43;"\t");}catch (Exception e){System.out.printf(e.getMessage());}}}

然后尽可能造多个不同的数字&#xff0c;默认保留0位小数&#xff0c;即scale为0&#xff0c;依次调用上面的demo方法&#xff1a;

BigDecimal aa &#61; new BigDecimal("3.300");BigDecimal bb &#61; new BigDecimal("3.344");BigDecimal cc &#61; new BigDecimal("3.355");BigDecimal dd &#61; new BigDecimal("3.356");BigDecimal ee &#61; new BigDecimal("3.366");BigDecimal ff &#61; new BigDecimal("3.512");BigDecimal gg &#61; new BigDecimal("3.561");BigDecimal hh &#61; new BigDecimal("3.169");BigDecimal ii &#61; new BigDecimal("3.000");BigDecimal jj &#61; new BigDecimal("3.001");BigDecimal ll &#61; new BigDecimal("3.500");BigDecimal mm &#61; new BigDecimal("4.500");BigDecimal aa1 &#61; new BigDecimal("-3.300");BigDecimal bb1 &#61; new BigDecimal("-3.344");BigDecimal cc1 &#61; new BigDecimal("-3.355");BigDecimal dd1 &#61; new BigDecimal("-3.356");BigDecimal ee1 &#61; new BigDecimal("-3.366");BigDecimal ff1 &#61; new BigDecimal("-3.512");BigDecimal gg1 &#61; new BigDecimal("-3.561");BigDecimal hh1 &#61; new BigDecimal("-3.169");BigDecimal ii1 &#61; new BigDecimal("-3.000");BigDecimal jj1 &#61; new BigDecimal("-3.001");BigDecimal ll1 &#61; new BigDecimal("-3.500");BigDecimal mm1 &#61; new BigDecimal("-4.500");demo(aa,0);demo(bb,0);demo(cc,0);demo(dd,0);demo(ee,0);demo(ff,0);demo(gg,0);demo(hh,0);demo(ii,0);demo(jj,0);demo(ll,0);demo(mm,0);System.out.println();System.out.println("------------------------------------------------------");demo(aa1,0);demo(bb1,0);demo(cc1,0);demo(dd1,0);demo(ee1,0);demo(ff1,0);demo(gg1,0);demo(hh1,0);demo(ii1,0);demo(jj1,0);demo(ll1,0);demo(mm1,0);

控制台输出&#xff08;格式化后的&#xff09;&#xff1a;

1、正数&#xff1a;

值 ROUND_UP ROUND_DOWN ROUND_CEILING ROUND_FLOOR ROUND_HALF_UP ROUND_HALF_DOWN ROUND_HALF_EVEN ROUND_UNNECESSARY3.300 4 3 4 3 3 3 3 Rounding necessary---------------------------------------------------------------------------------------------------------------------------------------------------------------------3.344 4 3 4 3 3 3 3 Rounding necessary---------------------------------------------------------------------------------------------------------------------------------------------------------------------3.355 4 3 4 3 3 3 3 Rounding necessary---------------------------------------------------------------------------------------------------------------------------------------------------------------------3.356 4 3 4 3 3 3 3 Rounding necessary---------------------------------------------------------------------------------------------------------------------------------------------------------------------3.366 4 3 4 3 3 3 3 Rounding necessary---------------------------------------------------------------------------------------------------------------------------------------------------------------------3.512 4 3 4 3 4 4 4 Rounding necessary---------------------------------------------------------------------------------------------------------------------------------------------------------------------3.561 4 3 4 3 4 4 4 Rounding necessary---------------------------------------------------------------------------------------------------------------------------------------------------------------------3.169 4 3 4 3 3 3 3 Rounding necessary---------------------------------------------------------------------------------------------------------------------------------------------------------------------3.000 3 3 3 3 3 3 3 3 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------3.001 4 3 4 3 3 3 3 Rounding necessary---------------------------------------------------------------------------------------------------------------------------------------------------------------------3.500 4 3 4 3 4 3 4 Rounding necessary---------------------------------------------------------------------------------------------------------------------------------------------------------------------4.500 5 4 5 4 5 4 4 Rounding necessary

2、负数 

值 ROUND_UP ROUND_DOWN ROUND_CEILING ROUND_FLOOR ROUND_HALF_UP ROUND_HALF_DOWN ROUND_HALF_EVEN ROUND_UNNECESSARY
-3.300 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.344 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.355 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.356 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.366 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.512 -4 -3 -3 -4 -4 -4 -4 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.561 -4 -3 -3 -4 -4 -4 -4 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.169 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.000 -3 -3 -3 -3 -3 -3 -3 -3
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.001 -4 -3 -3 -4 -3 -3 -3 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-3.500 -4 -3 -3 -4 -4 -3 -4 Rounding necessary
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
-4.500 -5 -4 -4 -5 -5 -4 -4 Rounding necessary

每种模式分别介绍下&#xff1a;

零、无论是正数还是负数都是往0以外的方向

/*** Rounding mode to round away from zero. Always increments the* digit prior to a nonzero discarded fraction. Note that this rounding* mode never decreases the magnitude of the calculated value.*/public final static int ROUND_UP &#61; 0;

一、无论是正数还是负数都是往0的方向

/*** Rounding mode to round towards zero. Never increments the digit* prior to a discarded fraction (i.e., truncates). Note that this* rounding mode never increases the magnitude of the calculated value.*/public final static int ROUND_DOWN &#61; 1;

二、区分正负数&#xff0c;正数变大&#xff0c;负数也是变大

/*** Rounding mode to round towards positive infinity. If the* {&#64;code BigDecimal} is positive, behaves as for* {&#64;code ROUND_UP}; if negative, behaves as for* {&#64;code ROUND_DOWN}. Note that this rounding mode never* decreases the calculated value.*/public final static int ROUND_CEILING &#61; 2;

三、区分正负数&#xff0c;正数变小&#xff0c;负数也是变小

/*** Rounding mode to round towards negative infinity. If the* {&#64;code BigDecimal} is positive, behave as for* {&#64;code ROUND_DOWN}; if negative, behave as for* {&#64;code ROUND_UP}. Note that this rounding mode never* increases the calculated value.*/public final static int ROUND_FLOOR &#61; 3;

四、正数小数位大于等于0.5则变大&#xff0c;负数小数位大于等于0.5则变小&#xff0c;注意这里是包含0.5

/*** Rounding mode to round towards {&#64;literal "nearest neighbor"}* unless both neighbors are equidistant, in which case round up.* Behaves as for {&#64;code ROUND_UP} if the discarded fraction is* ≥ 0.5; otherwise, behaves as for {&#64;code ROUND_DOWN}. Note* that this is the rounding mode that most of us were taught in* grade school.*/public final static int ROUND_HALF_UP &#61; 4;

五、正数小数位大于0.5则变大&#xff0c;负数小数位大于0.5则变小&#xff0c;注意这里是不包含0.5

/*** Rounding mode to round towards {&#64;literal "nearest neighbor"}* unless both neighbors are equidistant, in which case round* down. Behaves as for {&#64;code ROUND_UP} if the discarded* fraction is {&#64;literal >} 0.5; otherwise, behaves as for* {&#64;code ROUND_DOWN}.*/public final static int ROUND_HALF_DOWN &#61; 5;

六、此舍入模式也称为“银行家舍入法”&#xff0c;主要在美国使用。四舍六入&#xff0c;五分两种情况。如果前一位为奇数&#xff0c;则入位&#xff0c;否则舍去。

3.5->4但是4.5>4&#xff0c;因为3是奇数&#xff0c;4是偶数

/*** Rounding mode to round towards the {&#64;literal "nearest neighbor"}* unless both neighbors are equidistant, in which case, round* towards the even neighbor. Behaves as for* {&#64;code ROUND_HALF_UP} if the digit to the left of the* discarded fraction is odd; behaves as for* {&#64;code ROUND_HALF_DOWN} if it&#39;s even. Note that this is the* rounding mode that minimizes cumulative error when applied* repeatedly over a sequence of calculations.*/public final static int ROUND_HALF_EVEN &#61; 6;

七、断言是有精度的&#xff0c;如果没有会报异常

/*** Rounding mode to assert that the requested operation has an exact* result, hence no rounding is necessary. If this rounding mode is* specified on an operation that yields an inexact result, an* {&#64;code ArithmeticException} is thrown.*/public final static int ROUND_UNNECESSARY &#61; 7;


推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • Java中包装类的设计原因以及操作方法
    本文主要介绍了Java中设计包装类的原因以及操作方法。在Java中,除了对象类型,还有八大基本类型,为了将基本类型转换成对象,Java引入了包装类。文章通过介绍包装类的定义和实现,解答了为什么需要包装类的问题,并提供了简单易用的操作方法。通过本文的学习,读者可以更好地理解和应用Java中的包装类。 ... [详细]
author-avatar
爱辰teg_911
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有