使用BigDecimal 进行浮点值的精确计算

2019-01-28 10:27:16 浏览数 (1)

代码语言:javascript复制
import java.math.BigDecimal;

public class DecimalTest {

    public static void main(String[] args) {
        System.out.println("7.22-7.0=" subtractTest(7.2882,7.10));
    }

    private static double subtractTest(double v, double v1) {

        BigDecimal b1 = new BigDecimal(Double.toString(v));
        BigDecimal b2 = new BigDecimal(Double.toString(v1));
        System.out.println("b1=>" b1 " b2=>" b2);
        return b1.subtract(b2).doubleValue();
    }
}

输出打印为: b1=>7.2882 b2=>7.1 7.22-7.0=0.1882

0 人点赞