-
商业计算使用BigDecimal。
-
尽量使用参数类型为String的构造函数。
-
BigDecimal都是不可变的(immutable)的,在进行每一步运算时,都会产生一个新的对象,所以在做加减乘除运算时千万要保存操作后的值。
数值精确构造器
public BigDecimal(String val) {
this(val.toCharArray(), 0, val.length());
}
BigDecimal b1 = new BigDecimal("0.05");
public static BigDecimal valueOf(double val) {
// Reminder: a zero double returns '0.0', so we cannot fastpath
// to use the constant ZERO. This might be important enough to
// justify a factory approach, a cache, or a few private
// constants, later.
return new BigDecimal(Double.toString(val));
}
BigDecimal.valueOf(0.008);