learning java BigDecimal类
使用BiDecimal类是了为防止运算时精度丢失:
var f1 = new BigDecimal("0.05"); var f2 = BigDecimal.valueOf(0.01); var f3 = new BigDecimal(0.05); out.println(f1.add(f2)); out.println(f1.subtract(f2)); out.println(f1.multiply(f2)); out.println(f1.divide(f2));
output:
0.06 0.04 0.0005 5