BigDecimal.valueof() 与new BigDecimal()的区别

 

 

 

@Test
public void test() {
double d = 3.05;
BigDecimal b1 = new BigDecimal(d);
System.out.println(b1);//3.04999999999999982236431605997495353221893310546875
System.out.println(b1.setScale(1, RoundingMode.HALF_UP));//3.0
String str = "3.05";
BigDecimal b2 = new BigDecimal(str);
System.out.println(b2);//3.05
System.out.println(b2.setScale(1, RoundingMode.HALF_UP));//3.1
BigDecimal b3 = BigDecimal.valueOf(d);
System.out.println(b3);//3.05
System.out.println(b3.setScale(1, RoundingMode.HALF_UP));//3.1

}

上一篇:Excel查找匹配函数的16种方法


下一篇:编译和配置boost库