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

}

猜你喜欢

转载自www.cnblogs.com/corexy/p/12083711.html