When talking about commercial computing in Java, we all know that float and double cannot be used because they cannot perform exact computing. However, the Java designer provides programmers with a very useful class bigdecimal, which can improve the disadvantages that float and double classes cannot perform Exact computation. The bigdecimal class is located under the java. Maths class package. First, let's look at how to construct a bigdecimal object. There are many constructor functions. I will explain the two most commonly used constructors: bigdecimal (double Val) and bigdecimal (string Str ). There is no big difference between the two, but as described in the API description:
View plaincopy to clipboardprint?
/* The results of this constructor can be somewhat unpredictable. One might assume that
New bigdecimal (. 1) is exactly equal to. 1, but it is actually equal
To. 1000000000000000055511151231257827021181583404541015625. This is so because. 1
Cannot be represented exactly as a double (or, for that matter, as a binary Fraction
Of any finite length). Thus, the long value that is being passed in to the constructor
Is not exactly equal to. 1, appearances nonwithstanding.
The (string) constructor, on the other hand, is perfectly predictable: New bigdecimal
(". 1") is exactly equal to. 1, as one wocould could perform CT. Therefore, it is generally
Recommended that the (string) constructor be used in preference to this one .*/
/* The results of this constructor can be somewhat unpredictable. One might assume that
New bigdecimal (. 1) is exactly equal to. 1, but it is actually equal
To. 1000000000000000055511151231257827021181583404541015625. This is so because. 1
Cannot be represented exactly as a double (or, for that matter, as a binary Fraction
Of any finite length). Thus, the long value that is being passed in to the constructor
Is not exactly equal to. 1, appearances nonwithstanding.
The (string) constructor, on the other hand, is perfectly predictable: New bigdecimal
(". 1") is exactly equal to. 1, as one wocould could perform CT. Therefore, it is generally
Recommended that the (string) constructor be used in preference to this one .*/
That is to say, using double as the parameter constructor cannot accurately construct a bigdecimal object. You need to specify the context, that is, specify the exact bit. The string object can be used as the constructor passed in to accurately construct a bigdecimal object. See the following code:
View plaincopy to clipboardprint?
Import java. Math .*;
Public class testbigdecimal {
Public static void main (string ARGs []) {
Bigdecimal BD = new bigdecimal ("10.123 ");
Bigdecimal bd1 = newbigdecimal (10.123 );
System. Out. println (BD + "/N" + bd1 );
}
}
Import java. Math .*;
Public class testbigdecimal {
Public static void main (string ARGs []) {
Bigdecimal BD = new bigdecimal ("10.123 ");
Bigdecimal bd1 = newbigdecimal (10.123 );
System. Out. println (BD + "/N" + bd1 );
}
}
Output:
View plaincopy to clipboardprint?
10.123
10.1229999999999993320898283855058252811431884765625
10.123
10.1229999999999993320898283855058252811431884765625
Therefore, when selecting a constructor, it depends on the specific requirements.
In addition, many people will ask how to convert basic types, such as int, float, double, long, And bigdecimal objects. Simple:
The basic type is converted to the corresponding bigdecimal object through the constructor, while the bigdecimal class provides the functions such as intvalue (), floatvalue (), doublevalue (), and longvalue () to convert the bigdecimal object to the corresponding value.
As for how bigdecimal is calculated, I will take a post from a forum as an example to write a simple bigdecimal calculation method. The question is: Li Bai goes to the street without any problems and takes a pot to buy wine. When the shop doubled, see the flowers to drink a fight, five encounter flowers and shop, drink a pot of wine, try to ask how many fighting wine is there in the Li Bai pot?
This question should be pushed forward from the back, and Inverse Calculation is required to finally obtain the size of the original wine.
View plaincopy to clipboardprint?
Import java. Math .*;
Public class Libai {
Public static void main (string ARGs []) {
Bigdecimal volumn = new bigdecimal ("0 ");
For (INT I = 0; I <5; I ++ ){
Volumn = volumn. Add (New bigdecimal ("1 "));
Volumn = volumn. Divide (New bigdecimal ("2 "));
}
System. Out. Print (volumn );
}
}
Import java. Math .*;
Public class Libai {
Public static void main (string ARGs []) {
Bigdecimal volumn = new bigdecimal ("0 ");
For (INT I = 0; I <5; I ++ ){
Volumn = volumn. Add (New bigdecimal ("1 "));
Volumn = volumn. Divide (New bigdecimal ("2 "));
}
System. Out. Print (volumn );
}
}
Result:
View plaincopy to clipboardprint?
0.96875