我是靠谱客的博主 可靠仙人掌,最近开发中收集的这篇文章主要介绍BigDecimal 和 RoundingMode 相关备注,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

RoundingMode

UP:绝对值向上进位

0.1+0.02 = 0.12   ==>  0.2
精度设置为1,RoundingMode设置为UP
结果为0.2
(-0.1)+(-0.02) = -0.12    ==> -0.2
精度设置为1,RoundingMode设置为UP
结果为-0.2
System.out.println("==================正数=====================");
BigDecimal a1 = new BigDecimal("0.1");
BigDecimal b1 = new BigDecimal("0.02");
MathContext mc1 = new MathContext(1, RoundingMode.UP);
BigDecimal c1 = a1.add(b1,mc1);
System.out.println(c1.toString());  // 0.2

System.out.println("==================负数=====================");
BigDecimal a2 = new BigDecimal("-0.1");
BigDecimal b2 = new BigDecimal("-0.02");
MathContext mc2 = new MathContext(1, RoundingMode.DOWN);
BigDecimal c2 = a2.add(b2,mc2);
System.out.println(c2.toString());  // -0.2

DOWN:绝对值向下进位

0.1+0.06 = 0.16   ==>  0.1
精度设置为1,RoundingMode设置为DOWN
结果为0.1
(-0.1)+(-0.06) = -0.16    ==> -0.1
精度设置为1,RoundingMode设置为DOWN
结果为-0.1
System.out.println("==================正数=====================");
BigDecimal a1 = new BigDecimal("0.1");
BigDecimal b1 = new BigDecimal("0.06");
MathContext mc1 = new MathContext(1, RoundingMode.DOWN);
BigDecimal c1 = a1.add(b1,mc1);
System.out.println(c1.toString());  // 0.1

System.out.println("==================负数=====================");
BigDecimal a2 = new BigDecimal("-0.1");
BigDecimal b2 = new BigDecimal("-0.06");
MathContext mc2 = new MathContext(1, RoundingMode.DOWN);
BigDecimal c2 = a2.add(b2,mc2);
System.out.println(c2.toString());  //-0.1

FLOOR :数值向下进位

0.1+0.06 = 0.16   ==>  0.1
精度设置为1,RoundingMode设置为FLOOR 
结果为0.1
(-0.1)+(-0.02) = -0.12    ==> -0.2
精度设置为1,RoundingMode设置为FLOOR 
结果为-0.2
System.out.println("==================正数=====================");
BigDecimal a1 = new BigDecimal("0.1");
BigDecimal b1 = new BigDecimal("0.06");
MathContext mc1 = new MathContext(1, RoundingMode.FLOOR);
BigDecimal c1 = a1.add(b1,mc1);
System.out.println(c1.toString()); //0.1

System.out.println("==================负数=====================");
BigDecimal a2 = new BigDecimal("-0.1");
BigDecimal b2 = new BigDecimal("-0.02");
MathContext mc2 = new MathContext(1, RoundingMode.FLOOR);
BigDecimal c2 = a2.add(b2,mc2);
System.out.println(c2.toString());  // -0.2

CEILING :数值向上进位

0.1+0.02 = 0.12   ==>  0.2
精度设置为1,RoundingMode设置为CEILING
结果为0.2
(-0.1)+(-0.06) = -0.16    ==> -0.1
精度设置为1,RoundingMode设置为CEILING
结果为-0.1
System.out.println("==================正数=====================");
BigDecimal a1 = new BigDecimal("0.1");
BigDecimal b1 = new BigDecimal("0.02");
MathContext mc1 = new MathContext(1, RoundingMode.CEILING);
BigDecimal c1 = a1.add(b1,mc1);
System.out.println(c1.toString());  //0.2

System.out.println("==================负数=====================");
BigDecimal a2 = new BigDecimal("-0.1");
BigDecimal b2 = new BigDecimal("-0.06");
MathContext mc2 = new MathContext(1, RoundingMode.CEILING);
BigDecimal c2 = a2.add(b2,mc2);
System.out.println(c2.toString());  // -0.1

HALF_UP :绝对值传统四舍五入,0.5按绝对值向上进位

精度设置为1,RoundingMode设置为HALF_UP 
0.1+0.02 = 0.12   ==>  0.1
0.1+0.05 = 0.15   ==>  0.2
0.1+0.06 = 0.16   ==>  0.2
(-0.1)+(-0.02) = -0.12    ==> -0.1
(-0.1)+(-0.05) = -0.15    ==> -0.2 
(-0.1)+(-0.06) = -0.16    ==> -0.2
System.out.println("==================正数=====================");
BigDecimal a1 = new BigDecimal("0.1");
BigDecimal b1 = new BigDecimal("0.02");
BigDecimal d1 = new BigDecimal("0.05");
BigDecimal e1 = new BigDecimal("0.06");
MathContext mc1 = new MathContext(1, RoundingMode.HALF_UP);
System.out.println(a1.add(b1,mc1).toString());  
System.out.println(a1.add(d1,mc1).toString());
System.out.println(a1.add(e1,mc1).toString());

System.out.println("==================负数=====================");
BigDecimal a2 = new BigDecimal("-0.1");
BigDecimal b2 = new BigDecimal("-0.02");
BigDecimal d2 = new BigDecimal("-0.05");
BigDecimal e2 = new BigDecimal("-0.06");
MathContext mc2 = new MathContext(1, RoundingMode.HALF_UP);
System.out.println(a2.add(b2,mc2).toString());
System.out.println(a2.add(d2,mc2).toString());
System.out.println(a2.add(e2,mc2).toString());

HALF_DOWN :除0.5按绝对值向下进位外,类似于绝对值传统四舍五入

精度设置为1,RoundingMode设置为HALF_DOWN 
0.1+0.02 = 0.12   ==>  0.1
0.1+0.05 = 0.15   ==>  0.1
0.1+0.06 = 0.16   ==>  0.2
(-0.1)+(-0.02) = -0.12    ==> -0.1
(-0.1)+(-0.05) = -0.15    ==> -0.1
(-0.1)+(-0.06) = -0.16    ==> -0.2
System.out.println("==================正数=====================");
BigDecimal a1 = new BigDecimal("0.1");
BigDecimal b1 = new BigDecimal("0.02");
BigDecimal d1 = new BigDecimal("0.05");
BigDecimal e1 = new BigDecimal("0.06");
MathContext mc1 = new MathContext(1, RoundingMode.HALF_DOWN);
System.out.println(a1.add(b1,mc1).toString());
System.out.println(a1.add(d1,mc1).toString());
System.out.println(a1.add(e1,mc1).toString());

System.out.println("==================负数=====================");
BigDecimal a2 = new BigDecimal("-0.1");
BigDecimal b2 = new BigDecimal("-0.02");
BigDecimal d2 = new BigDecimal("-0.05");
BigDecimal e2 = new BigDecimal("-0.06");
MathContext mc2 = new MathContext(1, RoundingMode.HALF_DOWN);
System.out.println(a2.add(b2,mc2).toString());
System.out.println(a2.add(d2,mc2).toString());
System.out.println(a2.add(e2,mc2).toString());

HALF_EVEN :银行间舍入法 舍入部分为0.5时,与左右进位距离相等,则向偶数进位

精度设置为1,RoundingMode设置为HALF_EVEN 
0.1+0.02 = 0.12   ==>  0.1
0.1+0.05 = 0.15   ==>  0.2  (左右进位为0.1,0.2  其中2是偶数)
0.1+0.06 = 0.16   ==>  0.2
0.1+0.35 = 0.45   ==>  0.4  (左右进位为0.4,0.5  其中4是偶数)
(-0.1)+(-0.02) = -0.12    ==> -0.1
(-0.1)+(-0.05) = -0.15    ==> -0.2 
(-0.1)+(-0.06) = -0.16    ==> -0.2
(-0.1)+(-0.35) = -0.45    ==> -0.4
System.out.println("==================正数=====================");
 BigDecimal a1 = new BigDecimal("0.1");
 BigDecimal b1 = new BigDecimal("0.02");
 BigDecimal d1 = new BigDecimal("0.05");
 BigDecimal e1 = new BigDecimal("0.06");
 BigDecimal f1 = new BigDecimal("0.35");
 MathContext mc1 = new MathContext(1, RoundingMode.HALF_DOWN);
 System.out.println(a1.add(b1,mc1).toString());
 System.out.println(a1.add(d1,mc1).toString());
 System.out.println(a1.add(e1,mc1).toString());
 System.out.println(a1.add(f1,mc1).toString());

 System.out.println("==================负数=====================");
 BigDecimal a2 = new BigDecimal("-0.1");
 BigDecimal b2 = new BigDecimal("-0.02");
 BigDecimal d2 = new BigDecimal("-0.05");
 BigDecimal e2 = new BigDecimal("-0.06");
 BigDecimal f2 = new BigDecimal("-0.35");
 MathContext mc2 = new MathContext(1, RoundingMode.HALF_DOWN);
 System.out.println(a2.add(b2,mc2).toString());
 System.out.println(a2.add(d2,mc2).toString());
 System.out.println(a2.add(e2,mc2).toString());
 System.out.println(a2.add(f2,mc2).toString());

UNNECESSARY

  /**
     * 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.
     * Example:
     *Input rounded to one digit with {@code UNNECESSARY} rounding
     *5.5     throw {@code ArithmeticException}
     *2.5     throw {@code ArithmeticException}
     *1.6     throw {@code ArithmeticException}
     *1.1     throw {@code ArithmeticException}
     *1.0     1
     *-1.0   -1
     *-1.1    throw {@code ArithmeticException}
     *-1.6    throw {@code ArithmeticException}
     *-2.5    throw {@code ArithmeticException}
     *-5.5    throw {@code ArithmeticException}
     */
System.out.println("==================正数=====================");
BigDecimal a1 = new BigDecimal("0.1");
BigDecimal e1 = new BigDecimal("0.11");
MathContext mc1 = new MathContext(1, RoundingMode.UNNECESSARY);
System.out.println(a1.add(e1,mc1).toString());

/*输出:
Exception in thread "main" java.lang.ArithmeticException: Rounding necessary
    at java.math.BigDecimal.divideAndRound(BigDecimal.java:1452)
==================正数=====================
    at java.math.BigDecimal.doRound(BigDecimal.java:3752)
    at java.math.BigDecimal.add(BigDecimal.java:1189)
    at main.java.test.main(test.java:31)
*/

System.out.println("==================负数=====================");
BigDecimal a2 = new BigDecimal("-0.1");
BigDecimal e2 = new BigDecimal("-0.10");
MathContext mc2 = new MathContext(1, RoundingMode.UNNECESSARY);
System.out.println(a2.add(e2,mc2).toString());

/*输出:
==================正数=====================
0.2
*/

最后

以上就是可靠仙人掌为你收集整理的BigDecimal 和 RoundingMode 相关备注的全部内容,希望文章能够帮你解决BigDecimal 和 RoundingMode 相关备注所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部