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

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

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

        BigDecimal aa = new BigDecimal("3.300");BigDecimal bb = new BigDecimal("3.344");BigDecimal cc = new BigDecimal("3.355");BigDecimal dd = new BigDecimal("3.356");BigDecimal ee = new BigDecimal("3.366");BigDecimal ff = new BigDecimal("3.512");BigDecimal gg = new BigDecimal("3.561");BigDecimal hh = new BigDecimal("3.169");BigDecimal ii = new BigDecimal("3.000");BigDecimal jj = new BigDecimal("3.001");BigDecimal ll = new BigDecimal("3.500");BigDecimal mm = new BigDecimal("4.500");BigDecimal aa1 = new BigDecimal("-3.300");BigDecimal bb1 = new BigDecimal("-3.344");BigDecimal cc1 = new BigDecimal("-3.355");BigDecimal dd1 = new BigDecimal("-3.356");BigDecimal ee1 = new BigDecimal("-3.366");BigDecimal ff1 = new BigDecimal("-3.512");BigDecimal gg1 = new BigDecimal("-3.561");BigDecimal hh1 = new BigDecimal("-3.169");BigDecimal ii1 = new BigDecimal("-3.000");BigDecimal jj1 = new BigDecimal("-3.001");BigDecimal ll1 = new BigDecimal("-3.500");BigDecimal mm1 = 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);

控制台输出(格式化后的):

1、正数:

             值   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

每种模式分别介绍下:

零、无论是正数还是负数都是往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 =           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 =         1;

二、区分正负数,正数变大,负数也是变大

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

三、区分正负数,正数变小,负数也是变小

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

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

    /*** Rounding mode to round towards {@literal "nearest neighbor"}* unless both neighbors are equidistant, in which case round up.* Behaves as for {@code ROUND_UP} if the discarded fraction is* &ge; 0.5; otherwise, behaves as for {@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 =      4;

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

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

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

3.5->4但是4.5>4,因为3是奇数,4是偶数

    /*** Rounding mode to round towards the {@literal "nearest neighbor"}* unless both neighbors are equidistant, in which case, round* towards the even neighbor.  Behaves as for* {@code ROUND_HALF_UP} if the digit to the left of the* discarded fraction is odd; behaves as for* {@code ROUND_HALF_DOWN} if it'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 =    6;

七、断言是有精度的,如果没有会报异常

    /*** 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* {@code ArithmeticException} is thrown.*/public final static int ROUND_UNNECESSARY =  7;

BigDecimal的8种round舍入模式相关推荐

  1. java bigdecimal.round_down,java BigDecimal 的 setScale() 方法的 BigDecimal.ROUND_DOWN 舍入模式的BUG,坑...

    因为项目使用到 BigDecimal.ROUND_DOWN 接近零的舍入模式 ,没想到有一个 使用的坑. 下面来例子说明: String add = "67.80"; System ...

  2. BigDecimal 八种舍入模式介绍

    BigDecimal 八种舍入模式介绍 一.BigDecimal 简介 二.BigDecimal 的舍入模式 ROUND_UP ROUND_DOWN ROUND_CEILING ROUND_FLOOR ...

  3. java BigDecimal八种舍入模式

    一.BigDecimal介绍 java.math.BigDecimal 不可变的immutable.任意精度的有符号十进制数.BigDecimal 由任意精度的整数非标度值和32位的整数标度(scal ...

  4. java 向上舍入_介绍Java的大数类(BigDecimal)和八种舍入模式

    1.BigDecimal简介 BigDecimal 由任意精度的整数非标度值 和32 位的整数标度 (scale) 组成.如果为零或正数,则标度是小数点后的位数.如果为负数,则将该数的非标度值乘以 1 ...

  5. java 舍_Java中BigDecimal的8种舍入模式

    java.math.BigDecimal 不可变的.任意精度的有符号十进制数.BigDecimal 由任意精度的整数非标度值和32位的整数标度(scale)组成. 如果为零或正数,则标度是小数点后的位 ...

  6. BigDecimal参数8种舍入方式

    1.ROUND_UP 舍入远离零的舍入模式. 在丢弃非零部分之前始终增加数字(始终对非零舍弃部分前面的数字加1). 注意,此舍入模式始终不会减少计算值的大小. 2.ROUND_DOWN 接近零的舍入模 ...

  7. BigDecimal舍入模式(Rounding Modes)

    BigDecimal舍入模式(Rounding Modes) 1.ROUND_UP(向上舍入) 2.ROUND_DOWN(向下舍入) 3.ROUND_CEILING(以数轴向右舍入) 4.ROUND_ ...

  8. 全网最全的BigDecimal的newScale(保留小数位)和roundingMode(舍入模式),详细介绍roundingMode(重点)

    一般我们要对一个BigDecimal的数字进行保留小数位和设置舍入模式的时候,都是这样使用的: 对于加.减.乘得到的数字,直接使用BigDecimal对象.setScale,就像这样: 或者除法,在做 ...

  9. IEEE754标准中的4种舍入模式

    link 一.前言 最近在写一个基于IEEE754标准的浮点加法器,其中有一项要求就是要满足IEEE754标准的四种舍入模式. 我们在进行对阶或者右规格化的时候,阶数较小的操作数在进行右移的时候,会造 ...

最新文章

  1. 二分类问题:基于BERT的文本分类实践!附完整代码
  2. Node.js:get/post请求、全局对象、工具模块
  3. struts2拦截器遇到的问题
  4. 【微信小程序】 自定义导航栏(navigationStyle=custom)
  5. 【原创】SqlServer 2005 BCP命令详解
  6. 海奥华预言--第一章 神秘邀请
  7. 随笔编号-04 AngularJS 相关小问题解决方案合集
  8. (12)System Verilog 数组查找常数
  9. Java-Collection、List
  10. 【RobotStudio学习笔记】(六)有效载荷
  11. php中fread用法,php fread函数与fread函数用法
  12. 大数据统计分析架构-netty部分
  13. Hive 2.3.4 Name node is in safe mode. The reported blocks xxx has reached the threshold 0.9990 of to
  14. Android中仿微信选择图片并展示在RecyclerView中
  15. Mac键盘和Windows键盘对应表
  16. 基于SSM + MySQL的服饰服装购物平台系统的设计与实现源码+论文+包安装配置+讲解视频
  17. 被忽视的钣金零件外观设计
  18. paddle.fluid.io.xmap_readers
  19. 企业寄件分部门管理教程
  20. Java小程序 —— 简单五子棋

热门文章

  1. 二、5移动端网页适配
  2. 奔驰S400升级主动式氛围灯,大饼轮毂,4D旋转高音
  3. 2021-2027全球与中国厨房工作台安装服务市场深度研究分析报告
  4. Android双目三维重建:Android双目摄像头实现双目测距
  5. SDN网络中控制器RYU的安装
  6. odbc配置以及一个简单的java连接的代码编写
  7. The road to learning English-Grammar
  8. PNG、IconFont、SVG图标使用
  9. 《机器学习与数据科学(基于R的统计学习方法)》——1.2 机器学习的实际案例...
  10. Vue或uniapp使用luckysheet免费开发多人在线编辑Excel文档