如题对BigDecimal四舍五入的3个模式输出值的记录。
System.out.println("1.4/HALF_UP:"+new BigDecimal("1.4").setScale(0,BigDecimal.ROUND_HALF_UP));
System.out.println("1.5/HALF_UP:"+new BigDecimal("1.5").setScale(0,BigDecimal.ROUND_HALF_UP));
System.out.println("1.6/HALF_UP:"+new BigDecimal("1.6").setScale(0,BigDecimal.ROUND_HALF_UP));
System.out.println("---------------");
System.out.println("1.4/HALF_EVEN:"+new BigDecimal("1.4").setScale(0,BigDecimal.ROUND_HALF_EVEN));
System.out.println("1.5/HALF_EVEN:"+new BigDecimal("1.5").setScale(0,BigDecimal.ROUND_HALF_EVEN));
System.out.println("1.6/HALF_EVEN:"+new BigDecimal("1.6").setScale(0,BigDecimal.ROUND_HALF_EVEN));
System.out.println("---------------");
System.out.println("1.4/HALF_DOWN:"+new BigDecimal("1.4").setScale(0,BigDecimal.ROUND_HALF_DOWN));
System.out.println("1.5/HALF_DOWN:"+new BigDecimal("1.5").setScale(0,BigDecimal.ROUND_HALF_DOWN));
System.out.println("1.6/HALF_DOWN:"+new BigDecimal("1.6").setScale(0,BigDecimal.ROUND_HALF_DOWN));
System.out.println("---------------");
System.out.println("-1.4/HALF_UP:"+new BigDecimal("-1.4").setScale(0,BigDecimal.ROUND_HALF_UP));
System.out.println("-1.5/HALF_UP:"+new BigDecimal("-1.5").setScale(0,BigDecimal.ROUND_HALF_UP));
System.out.println("-1.6/HALF_UP:"+new BigDecimal("-1.6").setScale(0,BigDecimal.ROUND_HALF_UP));
System.out.println("---------------");
System.out.println("-1.4/HALF_EVEN:"+new BigDecimal("-1.4").setScale(0,BigDecimal.ROUND_HALF_EVEN));
System.out.println("-1.5/HALF_EVEN:"+new BigDecimal("-1.5").setScale(0,BigDecimal.ROUND_HALF_EVEN));
System.out.println("-1.6/HALF_EVEN:"+new BigDecimal("-1.6").setScale(0,BigDecimal.ROUND_HALF_EVEN));
System.out.println("---------------");
System.out.println("-1.4/HALF_DOWN:"+new BigDecimal("-1.4").setScale(0,BigDecimal.ROUND_HALF_DOWN));
System.out.println("-1.5/HALF_DOWN:"+new BigDecimal("-1.5").setScale(0,BigDecimal.ROUND_HALF_DOWN));
System.out.println("-1.6/HALF_DOWN:"+new BigDecimal("-1.6").setScale(0,BigDecimal.ROUND_HALF_DOWN));
结果
1.4/HALF_UP:1
1.5/HALF_UP:2
1.6/HALF_UP:2
---------------
1.4/HALF_EVEN:1
1.5/HALF_EVEN:2
1.6/HALF_EVEN:2
---------------
1.4/HALF_DOWN:1
1.5/HALF_DOWN:1
1.6/HALF_DOWN:2
---------------
-1.4/HALF_UP:-1
-1.5/HALF_UP:-2
-1.6/HALF_UP:-2
---------------
-1.4/HALF_EVEN:-1
-1.5/HALF_EVEN:-2
-1.6/HALF_EVEN:-2
---------------
-1.4/HALF_DOWN:-1
-1.5/HALF_DOWN:-1
-1.6/HALF_DOWN:-2
就结果看,UP跟EVEN都一样,应该可以满足正常用。简单记录,其中详细可以自查。