java precision problem

Do a simple backup first, then organize it later

package pri.lsx.test.bigdecimal;

import java.math.BigDecimal;

/**
 * Direct addition is not acceptable for reference http://www.cnblogs.com/chenssy/archive/2012/09/09/2677279.html
 *
 * @author lisx
 *@May 27, 2017
 */
public class Dome {

    public static void main(String[] args) {
        int a = 1;
        int b = 4;
        int c = a + b;
        double a2 = 1.0;
        double b2 = 4.0;
        double c2 = a2 + b2;
        double a3 = 1.000;
        double b3 = 4.000;
        double c3 = a3 + b3;
        double a4 = 1.1;
        double b4 = 4.1;
        double c4 = a4 + b4;

        //The running result of pure integer addition is also correct
        System.out.println(c);

        // The result of running a zero after the decimal point is also correct
        System.out.println(c2);

        //The result of running with three zeros after two decimals is specified
        System.out.println(c3); // equals 5.0 not 5.000

        //Only after the decimal point is a non-zero decimal, the result of this operation is wrong
        System.out.println(c4);
        System.out.println(0.06 + 0.01);
        System.out.println(1.0 - 0.42);
        System.out.println(4.015 * 100);
        System.out.println(303.1 / 1000);

// Try using the bigDecimal method below
        double a5 = 1.1;
        double b5 = 4.1;

// Perform precise four arithmetic operations on two double numbers with only one decimal place, this calculation method is invalid
        BigDecimal a51 = new BigDecimal(a5);
        BigDecimal b51 = new BigDecimal(b5);
        System.out.println(a51.add(b51).doubleValue()); //加法
        System.out.println(a51.add(b51).doubleValue());

// Calculate the two values ​​of a5 and b5 in another way, this way is the right way
        BigDecimal a52 = new BigDecimal(Double.toString(a5));
        BigDecimal b52 = new BigDecimal(Double.toString(b5));
        System.out.println("Double.toString is the correct method");
        System.out.println(a52.add(b52).doubleValue());

        // It is also possible to add two strings
        String str1 = new String("1.1");
        String stz1 = new String("4.1");
        BigDecimal str2 = new BigDecimal(str1);
        BigDecimal stz2 = new BigDecimal(stz1);
        System.out.println("But if it is a character type, Double.toString can not be used");
        System.out.println(str2.add(stz2).doubleValue());

// If the precision exceeds the precision of double, can it still be calculated accurately?
        double a6 = 1.111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111;
        double b6 = 4.111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111;
        System.out.println(a6 + b6); // direct addition will cause problems

        // Next, try the BigDecimal method
        BigDecimal a61 = new BigDecimal(Double.toString(a6));
        BigDecimal b62 = new BigDecimal(Double.toString(b6));
        System.out.println("Out of precision calculation, the Double.toString method will save the excess");
        System.out.println(a61.add(b62));
    }
}

Output result:

5
5.0
5.0
5.199999999999999
0.06999999999999999
0.5800000000000001
401.49999999999994
0.30310000000000004
5.199999999999999
5.199999999999999
Double.toString is the correct method
5.2
But if it is a character type, you can use the Double.toString method
5.2
5.222222222222221
For calculations beyond precision, the Double.toString method will save the excess
5.2222222222222222


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325371265&siteId=291194637