https://blog.csdn.net/jackiehff/article/details/8582449
一般使用String的构造方法,不然得出的double精度是不能保证的
double转换
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));
}
除法的策略
https://blog.csdn.net/qq_39164396/article/details/80992577
基本实现方法:取小数位数,然后找到小数位数多的那一个数,作为精度进行计算
减乘除其实最终都返回的是一个新的BigDecimal对象,因为BigInteger与BigDecimal都是不可变的(immutable)的,在进行每一步运算时,都会产生一个新的对象,所以a.add(b);虽然做了加法操作,但是a并没有保存加操作后的值,正确的用法应该是a=a.add(b);