大家好,又见面了,我是你们的朋友全栈君。
divide(BigDecimal divisor, int scale, int roundingMode)
参数 | 释义 |
---|---|
divisor | 被除数 |
scale | 保留小数位 |
roundingMode | 舍入模式 |
BigDecimal b1 = new BigDecimal("3");
BigDecimal b2 = new BigDecimal("7");
//计算b1除以b2的值并四舍五入保留两位小数
b1 = b1.divide(b2, 2 ,BigDecimal.ROUND_HALF_UP);
System.out.println("结果:"+b1);
结果:
结果:0.43
模式 | 释义 |
---|---|
ROUND_UP | 只要舍去的值不为0,则进1。如1.010保留一位小数为1.1,保留两位小数为1.01。 |
ROUND_DOWN | 无论舍去的值是多少,直接舍去(截断)。 |
ROUND_CEILING | 向正无穷方向舍入,若是正数则表现为’ROUND_UP’,若为负数则表现为’ROUND_DOWN’。 |
ROUND_FLOOR | 向负无穷方向舍入,若是正数则表现为’ROUND_DOWN’,若为负数则表现为ROUND_UP’’。 |
ROUND_HALF_UP | 四舍五入。 |
ROUND_HALF_DOWN | 五舍六入。 |
ROUND_HALF_EVEN | 若舍去位数左边值为偶数,则五舍六入。若舍去位数左边值为奇数,则四舍五入。 |
ROUND_UNNECESSARY | 断言得数为精确的结果,若不是则抛出ArithmeticException。 |
示例:
BigDecimal b1 = new BigDecimal("1.11");
BigDecimal b2 = new BigDecimal("1.10");
BigDecimal one = new BigDecimal("1");
b1 = b1.divide(one, 1 ,BigDecimal.ROUND_UP);
b2 = b2.divide(one, 1, BigDecimal.ROUND_UP);
System.out.println("b1结果:"+b1);
System.out.println("b2结果:"+b2);
结果:
b1结果:1.2
b2结果:1.1
示例:
BigDecimal b1 = new BigDecimal("1.11");
BigDecimal b2 = new BigDecimal("1.10");
BigDecimal one = new BigDecimal("1");
b1 = b1.divide(one, 1 ,BigDecimal.ROUND_DOWN);
b2 = b2.divide(one, 1, BigDecimal.ROUND_DOWN);
System.out.println("b1结果:"+b1);
System.out.println("b2结果:"+b2);
结果:
b1结果:1.1
b2结果:1.1
示例:
BigDecimal b1 = new BigDecimal("-1.11");
BigDecimal b2 = new BigDecimal("1.11");
BigDecimal one = new BigDecimal("1");
b1 = b1.divide(one, 1 ,BigDecimal.ROUND_CEILING);
b2 = b2.divide(one, 1, BigDecimal.ROUND_CEILING);
System.out.println("b1结果:"+b1);
System.out.println("b2结果:"+b2);
结果:
b1结果:-1.1
b2结果:1.2
示例:
BigDecimal b1 = new BigDecimal("-1.11");
BigDecimal b2 = new BigDecimal("1.11");
BigDecimal one = new BigDecimal("1");
b1 = b1.divide(one, 1 ,BigDecimal.ROUND_FLOOR);
b2 = b2.divide(one, 1, BigDecimal.ROUND_FLOOR);
System.out.println("b1结果:"+b1);
System.out.println("b2结果:"+b2);
结果:
b1结果:-1.2
b2结果:1.1
示例:
BigDecimal b1 = new BigDecimal("1.14");
BigDecimal b2 = new BigDecimal("1.15");
BigDecimal one = new BigDecimal("1");
b1 = b1.divide(one, 1 ,BigDecimal.ROUND_HALF_UP);
b2 = b2.divide(one, 1, BigDecimal.ROUND_HALF_UP);
System.out.println("b1结果:"+b1);
System.out.println("b2结果:"+b2);
结果:
b1结果:1.1
b2结果:1.2
示例:
BigDecimal b1 = new BigDecimal("1.15");
BigDecimal b2 = new BigDecimal("1.16");
BigDecimal one = new BigDecimal("1");
b1 = b1.divide(one, 1 ,BigDecimal.ROUND_HALF_DOWN);
b2 = b2.divide(one, 1, BigDecimal.ROUND_HALF_DOWN);
System.out.println("b1结果:"+b1);
System.out.println("b2结果:"+b2);
结果:
b1结果:1.1
b2结果:1.2
示例:
BigDecimal b1 = new BigDecimal("1.15");
BigDecimal b2 = new BigDecimal("1.25");
BigDecimal one = new BigDecimal("1");
b1 = b1.divide(one, 1 ,BigDecimal.ROUND_HALF_EVEN);
b2 = b2.divide(one, 1, BigDecimal.ROUND_HALF_EVEN);
System.out.println("b1结果:"+b1);
System.out.println("b2结果:"+b2);
结果:
b1结果:1.2
b2结果:1.2
结果精度确定:
BigDecimal b1 = new BigDecimal("10");
BigDecimal one = new BigDecimal("2");
b1 = b1.divide(one, 1 ,BigDecimal.ROUND_UNNECESSARY);
System.out.println("b1结果:"+b1);
结果:
b1结果:5.0
结果精度不确定:
BigDecimal b1 = new BigDecimal("3");
BigDecimal one = new BigDecimal("7");
b1 = b1.divide(one, 1 ,BigDecimal.ROUND_UNNECESSARY);
System.out.println("b1结果:"+b1);
结果:
Exception in thread "main" java.lang.ArithmeticException: Rounding necessary
at java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4148)
at java.math.BigDecimal.needIncrement(BigDecimal.java:4204)
at java.math.BigDecimal.divideAndRound(BigDecimal.java:4112)
at java.math.BigDecimal.divide(BigDecimal.java:5183)
at java.math.BigDecimal.divide(BigDecimal.java:1561)
at test.test.main(test.java:10)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/147669.html原文链接:https://javaforall.cn