热门标签 | 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;


推荐阅读
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 重入锁(ReentrantLock)学习及实现原理
    本文介绍了重入锁(ReentrantLock)的学习及实现原理。在学习synchronized的基础上,重入锁提供了更多的灵活性和功能。文章详细介绍了重入锁的特性、使用方法和实现原理,并提供了类图和测试代码供读者参考。重入锁支持重入和公平与非公平两种实现方式,通过对比和分析,读者可以更好地理解和应用重入锁。 ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
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社区 版权所有