BigDecimal b1 = new BigDecimal(123.123); //这种用法 123.123 将被视为 double 类型,产生精度丢失
BigDecimal b2 = BigDecimal.valueOf(123.123); //return new BigDecimal(Double.toString(val)); 源码先转成字符串,不会丢失精度
System.out.println(b1);
//123.1230000000000046611603465862572193145751953125
System.out.println(b2);
//123.123
new BigDecimal vs BigDecimal.valueOf
最新推荐文章于 2024-02-06 13:29:14 发布