Java BigDecimal and double examples and analysis of related problems

BigDecimal class

For numbers that do not require any accurate calculation accuracy, float or double can be used directly. However, if accurate calculation results are required, BigDecimal class must be used, and BigDecimal class can also be used for large numbers. The common methods of BigDecimal class are shown in Table 1.

Table 1 common methods of BigDecimal class

Example: rounding operation

BigDecimal is a class used to represent any precise floating-point operation in Java. In BigDecimal, unscaledvalue is used × 10 scale to represent a floating point number. Where unscaledvalue is a BigInteger and scale is an int. From this representation method, BigDecimal can only identify a limited number of decimals, but the data range that can be represented is much larger than double, which is basically enough in practical application.

Here are two precision problems

Question 1: the accuracy of BigDecimal (a guy on stackoverflow asked a related question)

Line 1: in fact, binary cannot accurately represent decimal decimal 0.1, but after reading the string "0.1", the compiler must convert it into an 8-byte double value. Therefore, the compiler can only replace 0.1 with the closest value, that is, 0.10000000000005551231257827021181583404541015625. Therefore, at run time, the real value passed to the BigDecimal constructor is 0.10000000000005551231257827021181583404541015625. Line 2: BigDecimal can correctly convert a string into a truly accurate floating-point number. Line 3: the problem is double ToString will use a certain precision to round double, and then output. meeting. Double. The output of toString (0.1000000000000 555111151231257827021181583404541015625) is actually "0.1", so the number represented by the generated BigDecimal is also 0.1. Line 4: Based on the previous analysis, in fact, this line of code is equivalent to the third line

Summary:

1. If you want BigDecimal to accurately represent the value you want, you must use a string to represent the decimal and pass it to the constructor of BigDecimal. 2. If you use double ToString transforms the double into a string and then calls BigDecimal (String). This is also not reliable. It does not necessarily work according to your idea. 3. If you don't care whether it is expressed exactly and BigDecimal (double) is used, you should pay attention to the special case of double itself. The specification of double defines several special double values (infinite, - infinite, Nan). Don't pass these values to BigDecimal, otherwise an exception will be thrown.

Question 2: don't you throw away the decimal part by forcibly converting double into int?

int x=(int)1023.99999999999999; // X = 1024 why?

The reason is that binary cannot accurately represent some decimal decimals, so the double value of 1023.9999999999999999 after compilation becomes 1024.

Therefore, the forced conversion of double to int is indeed to throw away the decimal part, but the value you write in the code is not necessarily the real double value generated by the compiler.

Verification Code:

double d = 1023.99999999999999; int x = (int) d; System. out. println(new BigDecimal(d). toString()); // 1024 System. out. println(Long.toHexString(Double.doubleToRawLongBits(d))); // 4090000000000000 System. out. println(x); // one thousand and twenty-four

As mentioned earlier, BigDecimal can accurately express double, remember.

We can also print out the binary form of D directly. According to IEEE 754, we can calculate 0x40900000000000 = (1024).

Here is another example of double to BigDecimal. The code is as follows:

The number is getting bigger.

Use (1) method to pass double directly to new BigDecimal, the number will become larger, (2) (3) no, it is recommended not to use (1)!

Here's a little joke:

"When I finish typing this line of code, I'll divorce you!" He said without raising his head. After listening, she felt warm in her heart. She thought that this might be the longest love commitment (because she knew that she could never finish the code) c2017 top ten moving story award

summary

The above is all about the analysis of Java BigDecimal and double examples and related problems in this article. I hope it will be helpful to you. Interested friends can continue to refer to this website:

Java programming BigDecimal usage example sharing

BigInteger of Java large number operation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>