Java 浮点数精确计算

示例代码

public class TestArithmetic {

    public static void main(String[] args) {
        System.out.println("0.09 + 0.01 = " + (0.09 + 0.01));
        System.out.println("9.99 - 0.12 = " + (9.99 - 0.12));
        System.out.println("17.173 * 100 = " + (17.173 * 100));
        System.out.println("16.3 / 1000 = " + (16.3 / 1000));
    }
}

运行结果

0.09 + 0.01 = 0.09999999999999999
9.99 - 0.12 = 9.870000000000001
17.173 * 100 = 1717.2999999999997
16.3 / 1000 = 0.016300000000000002

是不是怀疑以上结果写错了?但是结果确实是这样的,你可以亲自运行以上代码试试。

WHY?

为什么出现这种情况?因为 floatdouble 浮点类型在计算机中是无法准确表示的,例如 0.01 在计算机中只是表示成一个近似值,因此对于浮点数参与运算的结果会出现不准确的情况。

如何解决

使用 BigDecimal 进行精确计算

public class ArithmeticUtils {
    private static final int DEF_SCALE = 10;

    public static double add(double d1, double d2) {
        BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.add(b2).doubleValue();
    }

    public static double subtract(double d1, double d2) {
        BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.subtract(b2).doubleValue();
    }

    public static double multiply(double d1, double d2) {
        BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.multiply(b2).doubleValue();
    }

    public static double divide(double d1, double d2) {
        return divide(d1, d2, DEF_SCALE);
    }

    public static double divide(double d1, double d2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(d1));
        BigDecimal b2 = new BigDecimal(Double.toString(d2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}

再次运行以下代码

public class TestArithmetic {

    public static void main(String[] args) {
        System.out.println("0.09 + 0.01 = " + ArithmeticUtils.add(0.09, 0.01));
        System.out.println("9.99 - 0.12 = " + ArithmeticUtils.subtract(9.99, 0.12));
        System.out.println("17.173 * 100 = " + ArithmeticUtils.multiply(17.173, 100));
        System.out.println("16.3 / 1000 = " + ArithmeticUtils.divide(16.3, 1000));
    }
}

正确结果输出

0.09 + 0.01 = 0.1
9.99 - 0.12 = 9.87
17.173 * 100 = 1717.3
16.3 / 1000 = 0.0163

你可能感兴趣的:(Java 浮点数精确计算)