The problem of two double-added precision loss in Java and its solution

Source: Internet
Author: User

When discussing the addition of two double-digit 0.1 and 0.2, there is no doubt that the result of their addition is 0.2. But is the problem always the same?

Let's add the following two doubles numbers and look at the output:

    @Test    publicvoid  Testbig () {                System.out.println (0.11+2001299.32 );    }    

Console output 2001299.4300000002

We were surprised to find that the result was not what we had expected, but why? And how to solve it?

Now post a bigdecimal of a constructor of the document for everyone to refer to

BigDecimal
BigDecimal (Double val)
Will DoubleConverted to BigDecimal, the latter is DoubleThe exact decimal representation of the binary floating-point value. Returned by BigDecimalThe scale is to make (10scalexval)is the minimum value for the integer.

Note:

    1. The result of this construction method is somewhat unpredictable. One might think that the BigDecimal created by writing new BigDecimal (0.1) in Java is exactly equal to 0.1 (non-scale value 1, with a scale of 1), but it actually equals 0.100000000000000 0055511151231257827021181583404541015625. This is because 0.1 cannot be accurately represented as a double(or, for that case, it cannot be represented as any finite-length binary decimal). This way, the value passed into the construction method does not exactly equal 0.1 (although it is on the surface equal to the value).
    2. On the other hand, theString construction method is fully predictable: writing new BigDecimal ("0.1") creates a BigDecimal, which is exactly equal to the expected 0.1. Therefore, in comparison, it is generally advisable to use the String construction method first.
    3. When a double must be used as a source for BigDecimal , be aware that this construction method provides an accurate conversion; it does not provide the same result as the following: First use the Double.toString(double) method, and then use the BigDecimal(String) constructor method to The c16>double is converted to String. To get the result, use the static valueOf(double) method.

Parameters:
val-The double value to convert to BigDecimal .
Thrown:
NumberFormatException -If Val is infinitely large or NaN.

Workaround:

Believe from the above document everyone has also found out the solution,

When we need to accurately represent two decimal places, we need to convert them to BigDecimal objects and then perform the operations.

It is also important to note that there is still a loss of precision when using the BigDecimal (double Val) constructor, and it is recommended to use BigDecimal (String val), which

You need to convert the double to a string and then the argument as a BigDecimal (string Val) constructor. Converting to BigDecimal object before subtraction operation Precision is not

There will be a problem. That's why BigDecimal is used for money data storage.

To learn how BigDecimal works, please use the following Chinese documents

Http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/math/BigDecimal.html

The problem of two double-added precision loss in Java and its solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.