BigDecimal的setScale四大常用方法总结
// 设置小数点后第三位数字一大一小观察效果
BigDecimal num = new BigDecimal("3.3235667");
BigDecimal numOne = new BigDecimal("3.3275667");
1、ROUND_UP:进位制:不管保留数字后面是大是小(0除外)都会进1
// ROUND_UP--进位制:不管保留数字后面是大是小(0除外)都会进1
BigDecimal num1 = num.setScale(2,BigDecimal.ROUND_UP);
System.out.println("SmallROUND_UP:"+num1);
BigDecimal numTwo = numOne.setScale(2,BigDecimal.ROUND_UP);
System.out.println("BigROUND_UP:"+numTwo);
打印结果:SmallROUND_UP:3.33
BigROUND_UP:3.33
2、ROUND_DOWN:保留设置数字,后面所有直接去除
// ROUND_DOWN--保留设置数字,后面所有直接去除
BigDecimal num2 = num.setScale(2,BigDecimal.ROUND_DOWN);
System.out.println("SmallROUND_DOWN:"+num2);
BigDecimal numThree = numOne.setScale(2,BigDecimal.ROUND_DOWN);
System.out.println("BigROUND_DOWN:"+numThree);
打印结果:SmallROUND_DOWN:3.32
BigROUND_DOWN:3.32
3、ROUND_HALF_UP:根据保留数字后一位>=5进行四舍五入
// ROUND_HALF_UP--根据保留数字后一位>=5进行四舍五入
BigDecimal num3 = num.setScale(2,BigDecimal.ROUND_HALF_UP);
System.out.println("SmallROUND_HALF_UP:"+num3);
BigDecimal numFour = numOne.setScale(2,BigDecimal.ROUND_HALF_UP);
System.out.println("BigROUND_HALF_UP:"+numFour);
打印结果:SmallROUND_HALF_UP:3.32
BigROUND_HALF_UP:3.33
4、ROUND_HALF_DOWN:根据保留数字后一位>5进行四舍五入
BigDecimal num4 = num.setScale(2,BigDecimal.ROUND_HALF_DOWN);
System.out.println("SmallROUND_HALF_DOWN:"+num4);
BigDecimal numFive = numOne.setScale(2,BigDecimal.ROUND_HALF_DOWN);
System.out.println("BigROUND_HALF_DOWN:"+numFive);
打印结果:SmallROUND_HALF_DOWN:3.32标签:BigDecimal,UP,setScale,DOWN,HALF,ROUND From: https://www.cnblogs.com/wjsqqj/p/17361510.html
BigROUND_HALF_DOWN:3.33