The question is presented as follows:
What will we see if we compile and run the following program?
public class test{
public static void Main (String args[]) {
System.out.println (0.05+0.01);
System.out.println (1.0-0.42);
System.out.println (4.015*100);
System.out.println (123.3/100);
}
};
You didn't read it wrong! The result is indeed
0.060000000000000005
0.5800000000000001
401.49999999999994
1.2329999999999999
Simple floating-point type float and double in Java are not capable of operation. It's not just Java, it's also a problem in many other programming languages. In most cases, the results of the calculation are accurate, but try a few more times (you can do a loop) to try out similar errors. Now I finally understand why we have the BCD code.
This problem is quite serious, if you have 9.999999999999 yuan, your computer will not think you can buy 10 yuan of merchandise.
A specific currency type is provided in some programming languages to handle this situation, but Java does not. Now let's see how to solve this problem.
Rounded
Our first reaction was to do rounding. The round method in the math class cannot be set to hold a few decimals, we can only do this (keep two-bit):
Public double round (double value) {
Return Math.Round (value*100)/100.0;
}
Unfortunately, the code above does not work properly, pass 4.015 to this method and it will return 4.01 instead of 4.02, as we see above
4.015*100=401.49999999999994
So if we're going to do a precise rounding, we can't do anything with a simple type.
Java.text.DecimalFormat could not solve the problem:
System.out.println (Newjava.text.DecimalFormat ("0.00"). Format (4.025));
Output is 4.02
BigDecimal
This principle is also mentioned in the book "Effectivejava", where float and double can only be used for scientific calculations or engineering calculations, and we use java.math.BigDecimal in business calculations. BigDecimal There are 4 ways to make it, and we don't care about the two that we use BigInteger to build, then there are two, and they are:
BigDecimal (Doubleval)
Translatesadoubleintoabigdecimal.
BigDecimal (Stringval)
Translatesthestringrepresentationofabigdecimalintoabigdecimal.
The API outlined above is fairly clear, and usually the one above is easier to use.
We may not even want to use it, what is the problem? When there is a problem, it is found that there is a detailed description of the method in the above paragraph:
Note:theresultsofthisconstructorcanbesomewhatunpredictable. Onemight
Assumethatnewbigdecimal (. 1) isexactlyequalto.1,butitisactually
Equalto.1000000000000000055511151231257827021181583404541015625.thisisso
Because.1cannotberepresentedexactlyasadouble (Or,forthatmatter,as
Abinaryfractionofanyfinitelength). Thus,thelongvaluethatisbeing
Passedintotheconstructorisnotexactlyequalto.1,appearances
Nonwithstanding.
The (String) constructor,ontheotherhand,isperfectlypredictable:new
BigDecimal (". 1") Isexactlyequalto.1,asonewouldexpect. Therefore,itis
Generallyrecommendedthatthe (String) Constructorbeusedinpreferenceto
Thisone.
If we need to calculate accurately, we must use string to build BigDecimal! The example in "Effectivejava" is a string to make the BigDecimal, but the book does not emphasize this, which may be a small mistake.