JAVA BigDecimal 加减乘除算法、舍入模式以及去掉精度

java

BigDecimal firstNum=new BigDecimal("3");

BigDecimal secondNum=new BigDecimal("12");

//加法

System.out.println("thirdNum1 = firstNum + secondNum = "+firstNum.add(secondNum));//输出 15

System.out.println("thirdNum2 = secondNum + firstNum = "+secondNum.add(firstNum));//输出 15

//减法

System.out.println("thirdNum3 = firstNum - secondNum = "+firstNum.subtract(secondNum));//输出 -9

System.out.println("thirdNum4 = secondNum - firstNum = "+secondNum.subtract(firstNum));//输出 9

//乘法

System.out.println("thirdNum5 = firstNum * secondNum = "+firstNum.multiply(secondNum));//输出 36

System.out.println("thirdNum6 = secondNum * firstNum = "+secondNum.multiply(firstNum));//输出 36

//除法

System.out.println("thirdNum7 = firstNum / secondNum = "+firstNum.divide(secondNum,2,BigDecimal.ROUND_HALF_UP));//四舍五入,保留2位小数 输出 0.25

System.out.println("thirdNum8 = secondNum / firstNum = "+secondNum.divide(firstNum,2,BigDecimal.ROUND_HALF_UP));//四舍五入,保留2位小数 输出 4

BigDecimal fourthNum=new BigDecimal("11");

BigDecimal negativeNum=new BigDecimal("-11");

// firstNum / fourthNum = 0.27272727272727272727

// fourthNum / firstNum = 3.66666666666666666666

System.out.println("thirdNum9 = firstNum / fourthNum = "+firstNum.divide(fourthNum,2,BigDecimal.ROUND_UP));//始终对非零舍弃部分前面的数字加1,保留2位小数 输出 0.28

System.out.println("thirdNum10 = fourthNum / firstNum = "+fourthNum.divide(firstNum,2,BigDecimal.ROUND_UP));//始终对非零舍弃部分前面的数字加1,保留2位小数 输出 3.67

System.out.println("thirdNum11 = firstNum / fourthNum = "+firstNum.divide(fourthNum,2,BigDecimal.ROUND_DOWN));//截取前两位小数2位小数 输出 0.27

System.out.println("thirdNum12 = fourthNum / firstNum = "+fourthNum.divide(firstNum,2,BigDecimal.ROUND_DOWN));//截取前两位小数2位小数 输出 3.66

System.out.println("thirdNum13 = firstNum / fourthNum = "+firstNum.divide(fourthNum,2,BigDecimal.ROUND_CEILING));//thirdNum为正数,则等同于ROUND_UP 输出 0.28

System.out.println("thirdNum14 = fourthNum / firstNum = "+fourthNum.divide(firstNum,2,BigDecimal.ROUND_CEILING));//thirdNum为正数,则等同于ROUND_UP 输出 3.67

System.out.println("thirdNum15 = firstNum / negativeNum = "+firstNum.divide(negativeNum,2,BigDecimal.ROUND_CEILING));//thirdNum为负数,则等同于ROUND_DOWN 输出 -0.27

System.out.println("thirdNum16 = negativeNum / firstNum = "+negativeNum.divide(firstNum,2,BigDecimal.ROUND_CEILING));//thirdNum为负数,则等同于ROUND_DOWN 输出 -3.66

System.out.println("thirdNum17 = firstNum / fourthNum = "+firstNum.divide(fourthNum,2,BigDecimal.ROUND_FLOOR));//thirdNum为正数,则等同于ROUND_DOWN 输出 0.27

System.out.println("thirdNum18 = fourthNum / firstNum = "+fourthNum.divide(firstNum,2,BigDecimal.ROUND_FLOOR));//thirdNum为正数,则等同于ROUND_DOWN 输出 3.66

System.out.println("thirdNum19 = firstNum / fourthNum = "+firstNum.divide(negativeNum,2,BigDecimal.ROUND_FLOOR));//thirdNum为负数,则等同于ROUND_UP 输出 -0.28

System.out.println("thirdNum20 = fourthNum / firstNum = "+negativeNum.divide(firstNum,2,BigDecimal.ROUND_FLOOR));//thirdNum为负数,则等同于ROUND_UP 输出 -3.67

System.out.println("thirdNum21 = firstNum / fourthNum = "+firstNum.divide(fourthNum,2,BigDecimal.ROUND_HALF_DOWN));// 五舍六入 输出 0.27

System.out.println("thirdNum22 = fourthNum / firstNum = "+fourthNum.divide(firstNum,2,BigDecimal.ROUND_HALF_DOWN));// 五舍六入 输出 3.67

System.out.println("thirdNum23 = firstNum / fourthNum = "+firstNum.divide(fourthNum,2,BigDecimal.ROUND_HALF_EVEN));// 奇舍偶入,如0.2727,保留两位小数0.27,因7为奇数,故直接舍弃后面小数 输出 0.27

System.out.println("thirdNum24 = fourthNum / firstNum = "+fourthNum.divide(firstNum,2,BigDecimal.ROUND_HALF_EVEN));// 奇舍偶入,如3.6666666,保留两位小数3.66,因6为偶数,故后面小数入一位 输出 3.67

try{

//精准取值,不舍 若thirdNum的小数位小于4(精度位小于小数位),则会抛出异常ArithmeticException

System.out.println("thirdNum25 = firstNum / secondNum = "+firstNum.divide(secondNum,4,BigDecimal.ROUND_UNNECESSARY));// 不抛出异常 输出 0.2500

System.out.println("thirdNum26 = secondNum / firstNum = "+secondNum.divide(firstNum,4,BigDecimal.ROUND_UNNECESSARY));// 不抛出异常 输出 4.0000

System.out.println("thirdNum27 = firstNum / fourthNum = "+firstNum.divide(fourthNum,4,BigDecimal.ROUND_UNNECESSARY));// 抛出异常 Rounding necessary

System.out.println("thirdNum28 = fourthNum / firstNum = "+fourthNum.divide(firstNum,4,BigDecimal.ROUND_UNNECESSARY));// 抛出异常 Rounding necessary

}catch (ArithmeticException e){

System.out.println(e.getMessage());//Rounding necessary

}


//计算结果展示

 

//去掉精度

BigDecimal num = new BigDecimal("10.120000");

System.out.println(NumberUtil.toBigDecimal(NumberUtil.toStr(num)));

 

 





以上是 JAVA BigDecimal 加减乘除算法、舍入模式以及去掉精度 的全部内容, 来源链接: utcz.com/z/392605.html

回到顶部