• BigDecimal.setScale()方法用于商业计算的精度设置问题详解


    BigDecimal.setScale()方法用于商业计算的精度设置问题详解


        网上的说法繁杂,看起来诸多不便,并且有的说法也不太准确,在这里 做一下求证。用的较多的4个参数:
    BigDecimal.ROUND_UP 这个方法做直接进位处理,但是有个特殊情况需要考虑,如果将要舍弃的位上的值是0,不做进位处理。
    BigDecimal.ROUND_DOWN 这个方法做直接舍弃处理
    BigDecimal.ROUND_HALF_UP 这个方法就是大名鼎鼎的四舍五入

    BigDecimal.ROUND_HALF_DOWN 这个方法准确来说,是五舍六入

    1.  
      import java.math.BigDecimal;
    2.  
       
    3.  
      /**
    4.  
      *
    5.  
      * @author yangcq
    6.  
      * Java常用商业计算工具类BigDecimal的精度设置方法setScale()详解
    7.  
      *
    8.  
      */
    9.  
      public class BigDecimalTest {
    10.  
       
    11.  
      public static void main(String[] args) {
    12.  
      bigDecimalTest();
    13.  
      }
    14.  
       
    15.  
      private static void bigDecimalTest(){
    16.  
      BigDecimal bigDecimal_01 = new BigDecimal("918.845");
    17.  
      BigDecimal bigDecimal_02 = new BigDecimal("918.844");
    18.  
      BigDecimal bigDecimal_03 = new BigDecimal("918.846");
    19.  
       
    20.  
      bigDecimal_01.setScale(3);
    21.  
      bigDecimal_01.setScale(2,BigDecimal.ROUND_UP);
    22.  
      bigDecimal_01.setScale(2,BigDecimal.ROUND_DOWN);
    23.  
      bigDecimal_01.setScale(2,BigDecimal.ROUND_HALF_UP);
    24.  
      bigDecimal_01.setScale(2,BigDecimal.ROUND_HALF_DOWN);
    25.  
       
    26.  
      System.out.println("测试数据:" + bigDecimal_01);
    27.  
      System.out.println("BigDecimal.ROUND_UP = " + bigDecimal_01.setScale(2,BigDecimal.ROUND_UP));
    28.  
      System.out.println("BigDecimal.ROUND_DOWN = " + bigDecimal_01.setScale(2,BigDecimal.ROUND_DOWN));
    29.  
      System.out.println("BigDecimal.ROUND_HALF_UP = " + bigDecimal_01.setScale(2,BigDecimal.ROUND_HALF_UP));
    30.  
      System.out.println("BigDecimal.ROUND_HALF_DOWN = " + bigDecimal_01.setScale(2,BigDecimal.ROUND_HALF_DOWN));
    31.  
       
    32.  
      System.out.println("测试数据:" + bigDecimal_02);
    33.  
      System.out.println("BigDecimal.ROUND_UP = " + bigDecimal_02.setScale(2,BigDecimal.ROUND_UP));
    34.  
      System.out.println("BigDecimal.ROUND_DOWN = " + bigDecimal_02.setScale(2,BigDecimal.ROUND_DOWN));
    35.  
      System.out.println("BigDecimal.ROUND_HALF_UP = " + bigDecimal_02.setScale(2,BigDecimal.ROUND_HALF_UP));
    36.  
      System.out.println("BigDecimal.ROUND_HALF_DOWN = " + bigDecimal_02.setScale(2,BigDecimal.ROUND_HALF_DOWN));
    37.  
       
    38.  
      System.out.println("测试数据:" + bigDecimal_03);
    39.  
      System.out.println("BigDecimal.ROUND_UP = " + bigDecimal_03.setScale(2,BigDecimal.ROUND_UP));
    40.  
      System.out.println("BigDecimal.ROUND_DOWN = " + bigDecimal_03.setScale(2,BigDecimal.ROUND_DOWN));
    41.  
      System.out.println("BigDecimal.ROUND_HALF_UP = " + bigDecimal_03.setScale(2,BigDecimal.ROUND_HALF_UP));
    42.  
      System.out.println("BigDecimal.ROUND_HALF_DOWN = " + bigDecimal_03.setScale(2,BigDecimal.ROUND_HALF_DOWN));
    43.  
       
    44.  
      // BigDecimal.ROUND_UP 这个方法做直接进位处理,但是有个特殊情况需要考虑,如果将要舍弃的位上的值是0,不做进位处理。
    45.  
      BigDecimal bigDecimal_04 = new BigDecimal("918.850");
    46.  
      System.out.println("测试数据:" + bigDecimal_04);
    47.  
      System.out.println("BigDecimal.ROUND_UP = " + bigDecimal_04.setScale(2,BigDecimal.ROUND_UP));
    48.  
      System.out.println("BigDecimal.ROUND_DOWN = " + bigDecimal_04.setScale(2,BigDecimal.ROUND_DOWN));
    49.  
      System.out.println("BigDecimal.ROUND_HALF_UP = " + bigDecimal_04.setScale(2,BigDecimal.ROUND_HALF_UP));
    50.  
      System.out.println("BigDecimal.ROUND_HALF_DOWN = " + bigDecimal_04.setScale(2,BigDecimal.ROUND_HALF_DOWN));
    51.  
      }
    52.  
      }


    -------------------------------------控制台输出:----------------------------------------------

    测试数据:918.845
    BigDecimal.ROUND_UP = 918.85
    BigDecimal.ROUND_DOWN = 918.84
    BigDecimal.ROUND_HALF_UP = 918.85
    BigDecimal.ROUND_HALF_DOWN = 918.84
    测试数据:918.844
    BigDecimal.ROUND_UP = 918.85
    BigDecimal.ROUND_DOWN = 918.84
    BigDecimal.ROUND_HALF_UP = 918.84
    BigDecimal.ROUND_HALF_DOWN = 918.84
    测试数据:918.846
    BigDecimal.ROUND_UP = 918.85
    BigDecimal.ROUND_DOWN = 918.84
    BigDecimal.ROUND_HALF_UP = 918.85
    BigDecimal.ROUND_HALF_DOWN = 918.85
    测试数据:918.850
    BigDecimal.ROUND_UP = 918.85
    BigDecimal.ROUND_DOWN = 918.85
    BigDecimal.ROUND_HALF_UP = 918.85
    BigDecimal.ROUND_HALF_DOWN = 918.85

  • 相关阅读:
    在centos上搭建Git服务器
    glog日志库移植Android平台
    水葱
    路易斯安娜鸢尾
    再力花
    矮生百慕大
    洒金珊瑚
    八角金盘
    锦绣杜鹃
    茶梅球
  • 原文地址:https://www.cnblogs.com/cxy2020/p/13804440.html
Copyright © 2020-2023  润新知