java中的取整与四舍五入方法实例

2022-07-25,,,

一.java中取整数的方式

1.直接使用强制转换

public static void roundone(){
  system.out.println("正数:(int)10.12 = " + (int)10.12); 
  system.out.println("负数:(int)-10.12 = " + (int)-10.12); 
  system.out.println("---------------------------------");
  system.out.println("正数:(int)1011111111111111111111.12 = " + (int)1011111111111111111111.12); 
  system.out.println("负数:(int)-1011111111111111111111.12 = " + (int)-1011111111111111111111.12); 
 }

直接用强制转换的方式将浮点型数据转换为整型时,直接去掉小数点后部分的精度,取整数部分;直接强制取整有精度风险,一方面是小数位损失,另一方面当浮点型数字超过整型数字最大值时,会发生溢出。实际工程中,如果这两种因素都不会对工程产生影响,可以使用,否则不建议使用。

2.java中提供的取整的函数

java中提供了三种取整的函数:

  (1).math.ceil(double num);

  (2).math.floor(double num);

  (3).math.round(double num);

 public static void roundtwo(){
  system.out.println("正数:math.ceil(10.12) = " + math.ceil(10.12));
  system.out.println("负数:math.ceil(-10.12) = " + math.ceil(-10.12));
  system.out.println("正数:math.ceil(101111111111111111111.12) = " + math.ceil(101111111111111111111.12));
  system.out.println("负数:math.ceil(-101111111111111111111.12) = " + math.ceil(-101111111111111111111.12));
  system.out.println("---------------------------------");
  system.out.println("正数:math.floor(10.12) = " + math.floor(10.12));
  system.out.println("负数:math.floor(-10.12) = " + math.floor(-10.12));
  system.out.println("正数:math.floor(101111111111111111111.12) = " + math.floor(101111111111111111111.12));
  system.out.println("负数:math.floor(-101111111111111111111.12) = " + math.floor(-101111111111111111111.12));
 }

math.ceil(double num);函数是取浮点数的天花板数,即不小于num的最小整数;math.floor(double num)函数是取地板数,即不大于num的最大整数。这两个函数的返回值均是double型(java中当其值大于9999999.0时,默认用科学记数法表示),如果超过没有特殊情况,或者说规则很明确,就一种规则。

public static void roundthree(){
  system.out.println("小数点后第一位=5"); 
  system.out.println("正数:math.round(10.5) = " + math.round(10.5)); 
  system.out.println("负数:math.round(-10.5) = " + math.round(-10.5)); 
  system.out.println(); 
 
  system.out.println("小数点后第一位<5"); 
  system.out.println("正数:math.round(10.46) = " + math.round(10.46)); 
  system.out.println("负数:math.round(-10.46) = " + math.round(-10.46)); 
  system.out.println(); 
 
  system.out.println("小数点后第一位>5"); 
  system.out.println("正数:math.round(10.68) = " + math.round(10.68)); 
  system.out.println("负数:math.round(-10.68) = " + math.round(-10.68));
 }

math.round(double num)函数是取整函数,该函数只关注小数点后第一位小数值,具体规则如下:

(1).参数的小数点后第一位<5,运算结果为参数整数部分。

(2).参数的小数点后第一位>5,运算结果为参数整数部分绝对值+1,符号(即正负)不变。

(3).参数的小数点后第一位=5,正数运算结果为整数部分+1,负数运算结果为整数部分。

总结:大于五全部加,等于五正数加,小于五全不加。

二.java中四舍五入方法

1.使用bigdecimal对象的方式

public static void roundfour(){
  double f = 10.2345;
  bigdecimal b0 = new bigdecimal(f);
  bigdecimal b1 = new bigdecimal(f);
  bigdecimal b2 = new bigdecimal(f);
  bigdecimal b3 = new bigdecimal(f);
  bigdecimal b4 = new bigdecimal(f);
  bigdecimal b5 = new bigdecimal(f);
  bigdecimal b6 = new bigdecimal(f);
  bigdecimal b7 = new bigdecimal("10.2345");
  
  double f0 = b0.setscale(3, bigdecimal.round_up).doublevalue();
  double f1 = b1.setscale(3, bigdecimal.round_down).doublevalue();
  double f2 = b2.setscale(3, bigdecimal.round_ceiling).doublevalue();
  double f3 = b3.setscale(3, bigdecimal.round_floor).doublevalue();
  double f4 = b4.setscale(3, bigdecimal.round_half_up).doublevalue();
  double f5 = b5.setscale(3, bigdecimal.round_half_down).doublevalue();
  double f6 = b6.setscale(3, bigdecimal.round_half_even).doublevalue();
  double f7 = b7.setscale(4, bigdecimal.round_unnecessary).doublevalue();
  
  system.out.println(f + "使用 远离零方向舍入(round_up)方式四舍五入结果为:" + f0);
  system.out.println(f + "使用 趋向零方向舍入(round_down)方式四舍五入结果为:" + f1);
  system.out.println(f + "使用 向正无穷方向舍入(round_ceiling)方式四舍五入结果为:" + f2);
  system.out.println(f + "使用 向负无穷方向舍入(round_floor)方式四舍五入结果为:" + f3);
  system.out.println(f + "使用 最近数字舍入(5进)(round_half_up)方式四舍五入结果为:" + f4);
  system.out.println(f + "使用 最近数字舍入(5舍)(round_half_down)方式四舍五入结果为:" + f5);
  system.out.println(f + "使用 银行家舍入法(round_half_even)方式四舍五入结果为:" + f6);
  system.out.println(f + "使用 不需要舍入模式(round_unnecessary)方式结果为:" + f7);
 }

bigdecimal中有8中四舍五入设置方式:

(1).round_up:远离零方向舍入。向绝对值最大的方向舍入,只要舍弃位非0即进位。

(2).round_down:趋向零方向舍入。向绝对值最小的方向输入,所有的位都要舍弃,不存在进位情况。

(3).round_ceiling:向正无穷方向舍入。向正最大方向靠拢。若是正数,舍入行为类似于round_up,若为负数,舍入行为类似于round_down。math.round()方法就是使用的此模式。

(4).round_floor:向负无穷方向舍入。向负无穷方向靠拢。若是正数,舍入行为类似于round_down;若为负数,舍入行为类似于round_up。

(5).round_half_up:最近数字舍入(5进)。这是我们最经典的四舍五入。

(6).round_half_down:最近数字舍入(5舍)。在这里5是要舍弃的。

(7).round_half_even:银行家舍入法。

(8).round_unnecessary:计算结果是精确的,不需要舍入模式。

    a.round_half_down解释

    第(6)中四舍五入方式round_half_down解释的是遇到5要舍弃,但10.2345保留3位小数后结果是10.235,并没有直接舍去精确位的5,还是进了1,为什么呢?

public static void roundfive(){
  //通过double类型作为参数实例化bigdecimal对象
  double f = 10.2345;
  bigdecimal b5 = new bigdecimal(f);
  system.out.println("b5:" + b5);
  double f5 = b5.setscale(3, bigdecimal.round_half_down).doublevalue();
  system.out.println("round_half_down方式处理后:" + f5);
  system.out.println("----------------------------");
  //通过字符串类型作为参数实例化bigdecimal对象
  bigdecimal b5_1 = new bigdecimal("10.2345");
  system.out.println("b5_1:" + b5_1);
  double f5_1 = b5_1.setscale(3, bigdecimal.round_half_down).doublevalue();
  system.out.println("round_half_down方式处理后:" + f5_1);
  system.out.println("----------------------------");
  //遇到的5后面有小数位0,对数据大小无影响,直接舍弃5
  bigdecimal b5_2 = new bigdecimal("10.23450");
  system.out.println("b5_2:" + b5_2);
  double f5_2 = b5_2.setscale(3, bigdecimal.round_half_down).doublevalue();
  system.out.println("round_half_down方式处理后:" + f5_2);
  system.out.println("----------------------------");
  
  //遇到的5后面有非0小数位,对数据大小有影响,会进1
  bigdecimal b5_3 = new bigdecimal("10.234501");
  system.out.println("b5_3:" + b5_3);
  double f5_3 = b5_3.setscale(3, bigdecimal.round_half_down).doublevalue();
  system.out.println("round_half_down方式处理后:" + f5_3);
  system.out.println("----------------------------");
 }

    通过例子可以看出,10.2345通过round_half_down方式保留三位小数后结果为10.235原因是,使用的double类型初始化产生的bigdecimal对象的,实际上实例化后的数值并不是10.2345,而是10.234500000000000596855898038484156131744384765625,根据样例可知,当5后面还有其他小数时,依然会向前进1位。也就是说当使用round_half_down方式时,并不是所有的5都直接舍去,需要看5后面是否有其他非0位,如果没有,直接舍去,如果有,需要进1。

    b.银行家算法

    四舍五入其实在金融方面运用的非常多,尤其是银行的利息。我们都知道银行的盈利渠道主要是利息差,它从储户手里收集资金,然后放贷出去,期间产生的利息差就是银行所获得的利润。如果我们采用平常四舍五入的规则话,这里采用每10笔存款利息计算作为模型,如下:

四舍:0.000、0.001、0.002、0.003、0.004。这些舍的都是银行赚的钱。

五入:0.005、0.006、0.007、0.008、0.009。这些入的都是银行亏的钱,

分别为:0.005、0.004、.003、0.002、0.001。

    所以对于银行来说它的盈利应该是0.000 + 0.001 + 0.002 + 0.003 + 0.004 - 0.005 - 0.004 - 0.003 - 0.002 - 0.001 = -0.005。从结果中可以看出每10笔的利息银行可能就会损失0.005元,千万别小看这个数字,这对于银行来说就是一笔非常大的损失。面对这个问题就产生了如下的银行家涉入法了。该算法是由美国银行家提出了,主要用于修正采用上面四舍五入规则而产生的误差。如下:

(1).舍去位的数值小于5时,直接舍去。

(2).舍去位的数值大于5时,进位后舍去。

(3).当舍去位的数值等于5时,若5后面还有其他非0数值,则进位后舍去,若5后面是0时,则根据5前一位数的奇偶性来判断,奇数进位,偶数舍去。

对于上面的规则我们举例说明

11.556 = 11.56          ------六入
11.554 = 11.55          -----四舍
11.5551 = 11.56         -----五后有数进位
11.545 = 11.54          -----五后无数,若前位为偶数应舍去
11.555 = 11.56          -----五后无数,若前位为奇数应进位

    c.round_unnecessary解释

   round_unnecessary方式表示计算结果是精确的,如果不是精确的,将会抛出java.lang.arithmeticexception异常。

public static void roundsix(){
  bigdecimal b7 = new bigdecimal("10.23455");
  double f7 = b7.setscale(4, bigdecimal.round_unnecessary).doublevalue();
  system.out.println("round_unnecessary方式处理后:" + f7);
 }

如果将bigdecimal b7 = new bigdecimal("10.23455")中的数字改为10.2345或10.234500000;即可正常运行。也就是说,使用round_unnecessary方式时,浮点数保留n位小数时,不能影响数字的精度,只要有舍弃掉数字导致精度受影响,都会抛出异常。

注:这些枚举值有时候会用roundingmode类中的枚举值,其实效果是一样的,roundingmode只是将bigdecimal中的枚举又封装了一层,简化了一下枚举名,无实质性差别。roundingmode枚举类中的枚举示例:up(bigdecimal.round_up),

2.使用decimalformat对象的方式

public static void roundseven(){
  decimalformat df = new decimalformat("#.000");
  //df.setroundingmode(roundingmode.down);
  system.out.println(df.format(new bigdecimal(10.2345)));//10.235
 }

注:decimalformat默认采用了roundingmode.half_even这种类型,可以通过setroundingmode方法进行设置四舍五入方式,而且format之后的结果是一个字符串类型string。

3.使用string.format方式

public static void roundeight(){
  double d = 10.2345;
  string result = string.format("%.3f", d);
  system.out.println("result:" + result);
 }

输出为10.235。string.format可以格式化很多类型的数据,包括整数、浮点数、字符串、日期等,具体对浮点数的格式化规则后续详细介绍,此处只需知道浮点数的四舍五入有这种方式。

4.使用math.round方式

public static void roundnine(){
  double d1 = math.round(5.2644555*100)*0.01d;
  system.out.println("d1:" + d1);
  
  double d2 = math.round(5.2654555*100)*0.01d;
  system.out.println("d2:" + d2);
 }

math.round()方式不建议使用,因为会有风险,如样例所示。

5.使用numberformat方式

public static void roundten(){
  double d = 10.2345;
  numberformat nf=numberformat.getnumberinstance() ;
  nf.setmaximumfractiondigits(2);
  string s= nf.format(d) ;
  system.out.println("s1:" + s);
  
  nf.setmaximumfractiondigits(3);
  s= nf.format(d) ;
  system.out.println("s2:" + s);
 }

总结

到此这篇关于java中取整与四舍五入的文章就介绍到这了,更多相关java取整与四舍五入内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《java中的取整与四舍五入方法实例.doc》

下载本文的Word格式文档,以方便收藏与打印。