关于小数的精确运算

package test;

import java.math.BigDecimal;

public class Test {

    public static void main(String[] args) {
        //double 只适合做科学运算,如果要进行精确运算,是不能用double来做的
        double a = 0.1;
        double b = 0.006;
        System.out.println(a+b);//0.10600000000000001
        
        //要向精确运算就要用BigDecimal
        BigDecimal a1 = new BigDecimal("0.1");
        BigDecimal b1 = new BigDecimal("0.006");
        //+
        System.out.println(a1.add(b1).toString());//0.106
        //*
        System.out.println(a1.multiply(b1).toString());
        //除   要写保留的小数点位数
        System.out.println(a1.divide(b1,5,BigDecimal.ROUND_HALF_UP).toString());
    }

}

原文地址:https://www.cnblogs.com/siashan/p/3918190.html