背景:用bigdecimal,结果出现一大堆小数
public static void bigDcimalDot() {
System.out.println(new BigDecimal(3.1415926));
//打印结果:3.14159260000000006840537025709636509418487548828125
System.out.println(new BigDecimal("3.1415926"));
//打印结果:3.1415926
//描述:double转为Bigdecimal,位数过多转换时变为科学计数法,容易出现精度丢失现象
//解决方案:如下方法先转为字符串再转为BigDecimal
System.out.println(new BigDecimal(3.1415926).setScale(7,BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString());
//打印结果:3.1415926
}