1. BigDecimal num1 = new BigDecimal(2.225667);//这种写法不允许,会造成精度损失
2. BigDecimal num2 = new BigDecimal(2);//这种写法是可以的
3. BigDecimal num = new BigDecimal("2.225667");//一般都会这样写最好
4. int count = num.scale();
System.out.println(count);//6 返回的是小数点后位数
好了,下面开始正式介绍知识点啦~~~~~~
1. ROUND_DOWN
-
BigDecimal b = new BigDecimal("2.225667").setScale(2, BigDecimal.ROUND_DOWN);
-
2. ROUND_UP
-
BigDecimal c = new BigDecimal("2.224667").setScale(2, BigDecimal.ROUND_UP);
-
3. ROUND_CEILING
天花板(向上),正数进位向上,负数舍位向上
-
BigDecimal f = new BigDecimal("2.224667").setScale(2, BigDecimal.ROUND_CEILING);
-
-
-
BigDecimal g = new BigDecimal("-2.225667").setScale(2, BigDecimal.ROUND_CEILING);
-
4. ROUND_FLOOR
地板(向下),正数舍位向下,负数进位向下
-
BigDecimal h = new BigDecimal("2.225667").setScale(2, BigDecimal.ROUND_FLOOR);
-
-
-
BigDecimal i = new BigDecimal("-2.224667").setScale(2, BigDecimal.ROUND_FLOOR);
-
5. ROUND_HALF_UP
-
BigDecimal d = new BigDecimal("2.225").setScale(2, BigDecimal.ROUND_HALF_UP);
-
System.out.println("ROUND_HALF_UP"+d);
6. ROUND_HALF_DOWN
-
BigDecimal e = new BigDecimal("2.225").setScale(2, BigDecimal.ROUND_HALF_DOWN);
-
System.out.println("ROUND_HALF_DOWN"+e);
7. ROUND_HALF_EVEN
-
BigDecimal j = new BigDecimal("2.225").setScale(2, BigDecimal.ROUND_HALF_EVEN);
-
-
-
BigDecimal k = new BigDecimal("2.215").setScale(2, BigDecimal.ROUND_HALF_EVEN);
-
-
-
-
System.out.println("************************************");
-
-
System.out.println("4.05: "+new BigDecimal("4.05").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("4.15: "+new BigDecimal("4.15").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("4.25: "+new BigDecimal("4.25").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("4.35: "+new BigDecimal("4.35").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("4.45: "+new BigDecimal("4.45").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("4.55: "+new BigDecimal("4.55").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("4.65: "+new BigDecimal("4.65").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
-
System.out.println("3.05: "+new BigDecimal("3.05").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("3.15: "+new BigDecimal("3.15").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("3.25: "+new BigDecimal("3.25").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("3.35: "+new BigDecimal("3.35").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("3.45: "+new BigDecimal("3.45").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("3.55: "+new BigDecimal("3.55").setScale(1, BigDecimal.ROUND_HALF_EVEN));
-
System.out.println("3.65: "+new BigDecimal("3.65").setScale(1, BigDecimal.ROUND_HALF_EVEN));
8.ROUND_UNNECESSARY
-
BigDecimal l = new BigDecimal("2.215").setScale(3, BigDecimal.ROUND_UNNECESSARY);
-
-
-