doubleValue(), toString(), toPlainString(), and scientific counting method of 87 ° BigDecimal

Keywords: Java

There is a small partner on the project who sends the result to the foreground in the way of return num.doubleValue() + ""; when using Bigdecimal for numerical calculation. When the test value is small, there is no problem. When the actual project is running, the value is large, and the content displayed on the result page becomes the form of scientific counting. After finding the problem, another small partner changes it to return num.toString(); and the result shows that it is back to normal. By chance, when I saw this code, I found this error. I remember that toString () will become a scientific counting method in some specific values, while the method of toplaningstring () will never change, and the original value of Bigdecimal will always be displayed. In order to persuade my little friend, I spent a few minutes writing a test method. The code is as follows:

import java.math.BigDecimal;
 
public class Test {
 
    public static void main(String[] args) {
        BigDecimal a1 = new BigDecimal("1");
        // Scale up
        for (int i = 0; i < 20; i++) {
            a1 = a1.multiply(new BigDecimal("0.1"));
            System.out.println("----------------------" + (i + 1) + "Output with decimal places-------------------------");
            System.out.println("doubleValue Method display value:                         " + a1.doubleValue());
            System.out.println("toString Method display value:                                 " + a1.toString());
            System.out.println("toPlainString Method display value:                    " + a1.toPlainString());
        }
        System.out.println("\n\n");
        a1 = new BigDecimal("1");
        // Increasing the number of integers
        for (int i = 1; i < 20; i++) {
            a1 = a1.multiply(new BigDecimal("10"));
            System.out.println("----------------------" + (i + 1) + "Output result when bit integer-------------------------");
            System.out.println("doubleValue Method display value:                         " + a1.doubleValue());
            System.out.println("toString Method display value:                                 " + a1.toString());
            System.out.println("toPlainString Method display value:                    " + a1.toPlainString());
        }
        System.out.println("\n\n");
        // Always two decimal places, increasing integer bits
        a1 = new BigDecimal("1");
        for (int i = 1; i < 30; i++) {
            a1 = a1.setScale(0, BigDecimal.ROUND_DOWN).multiply(new BigDecimal("10.11"));
            System.out.println("----------------------" + i + "Bit integer,2 Output with decimal places-------------------------");
            System.out.println("doubleValue Method display value:                         " + a1.doubleValue());
            System.out.println("toString Method display value:                                 " + a1.toString());
            System.out.println("toPlainString Method display value:                    " + a1.toPlainString());
        }
        System.out.println("\n\n");
        // Decimal places and integer bits increase at the same time
        a1 = new BigDecimal("1");
        for (int i = 1; i < 30; i++) {
            if (i % 2 == 0) {
                a1 = a1.multiply(new BigDecimal("10"));
            } else {
                a1 = a1.multiply(new BigDecimal("10.1"));
            }
            System.out.println(
                    "----------------------" + (i + 1) + "Bit integer," + ((i + 1) / 2) + "Output with decimal places-------------------------");
            System.out.println("doubleValue Method display value:                         " + a1.doubleValue());
            System.out.println("toString Method display value:                                 " + a1.toString());
            System.out.println("toPlainString Method display value:                    " + a1.toPlainString());
        }
    }
}


The operation results are as follows:

At 4 decimal places, the doubleValue() display changes to scientific counting. toString() becomes the scientific method of counting at 7 decimal places. toPlainString() is always unchanged.

When an 8-bit integer is used, the doubleValue() display changes to scientific counting.

When the number of integers and decimals increases, doubleValue() becomes a scientific counting method, while the other two methods remain unchanged.

From the above tests, it is concluded that toString() will change the displayed value into scientific counting method in some cases, while toPlainString() will always accurately display the value itself.

Original: https://blog.csdn.net/u013742303/article/details/80591187

Posted by Barnacles on Sat, 16 Nov 2019 13:07:03 -0800