BigDecimal is a class in Java that represents an arbitrary precision floating-point number operation, and in BigDecimal, the Unscaledvaluex10-scale is used to represent a floating-point number. Where Unscaledvalue is a biginteger,scale is an int. From this point of view, BigDecimal can only identify finite decimals, but can represent a range of data far greater than double, in practical applications is basically enough.
Let's mention two accuracy questions:question one: BigDecimal's accuracy problem (a guy on StackOverflow asked a related question)
System.out.println (New BigDecimal (0.1). ToString ()); 0.1000000000000000055511151231257827021181583404541015625system.out.println (New BigDecimal ("0.1"). ToString ()) ; 0.1system.out.println (New BigDecimal (Double.tostring ( 0.1000000000000000055511151231257827021181583404541015625)). toString ());//0.1system.out.println (New BigDecimal ( Double.tostring (0.1)). ToString ()); 0.1
Analyze the problem with the above code (the contents of the comment represent the output of this statement)
The first line: In fact, since the binary cannot accurately represent decimal decimal 0.1, but after the compiler reads the string "0.1", it must be converted to a double value of 8 bytes, so The compiler can use only one nearest value instead of 0.1, or 0.1000000000000000055511151231257827021181583404541015625. Thus, at run time, the true value passed to the BigDecimal constructor is 0.1000000000000000055511151231257827021181583404541015625. Second line: BigDecimal can correctly convert the string into a truly accurate floating-point number. The third line: The problem is that Double.tostring uses a certain amount of precision to round the double and then output it. Yes. The double.tostring (0.1000000000000000055511151231257827021181583404541015625) output is actually "0.1", So the number of BigDecimal represented is also 0.1. Line four: Based on the previous analysis, the fact that this line of code is equivalent to the third line conclusion: 1. If you want BigDecimal to be able to accurately represent the value you want, be sure to use a string to represent the decimal and pass it to the BigDecimal constructor. 2. If you use double.tostring to convert a double into a string and then call BigDecimal (String), this is also not reliable, it does not necessarily work as you think. 3. If you are not very concerned about whether you are fully accurate and use BigDecimal (double), pay attention to the special case of the double itself, the specification of the double itself defines several specific double values (Infinite,-infinite,nan ), do not pass these values to BigDecimal, or you will throw an exception.question two: Is it not to throw away the fractional part by forcing the double into int?
int x= (int) 1023.99999999999999; x=1024 Why?
The reason is that the binary cannot accurately represent some decimal decimals, so the double value of 1023.99999999999999 after compilation becomes 1024.
So, forcing a double into int is really throwing away the fractional part, but the value you write in the code is not necessarily the true double value that the compiler generates. Verification Code:
Double d = 1023.99999999999999;int x = (int) d; System.out.println (New BigDecimal (d). toString ()); 1024system.out.println (Long.tohexstring (double.doubletorawlongbits (d))); 4090000000000000SYSTEM.OUT.PRINTLN (x); 1024
As mentioned above, BigDecimal can be accurately represented by a double, remember.
We can also print the binary form of D directly, according to IEEE 754, we can calculate the 0x4090000000000000= (1024).
Java BigDecimal and double