Introduction to BigDecimal
The explanation in the JDK document (Chinese) is as follows:
Immutable, signed decimal number of arbitrary precision. BigDecimal consists of an integer non-scale value of any precision and a 32-bit integer scale. If it is zero or positive, the scale is the number of digits after the decimal point. If it is a negative number, multiply the non-scaling value of the number by a negative scale power of 10. Therefore, the value represented by BigDecimal is (unscaledValue × 10-scale).
Specific explanation
1. "The value of the BigDecimal object is immutable". This shows this feature in the operation function of the BigDecimal object:
The code copy is as follows: BigDecimal a = new BigDecimal("1.22");
System.out.println("construct with a String value: " + a);
BigDecimal b = new BigDecimal("2.22");
a.add(b);
System.out.println("a plus b is : " + a);
It's easy to think that it will output:
construct with a Stringvalue: 1.22
a plus b is :3.44
But in fact a plus b is : 1.22
2. "BigDecimal consists of an integer non-scale value of any precision and a 32-bit integer scale. If the scale value is zero or positive, the scale is the number of digits after the decimal point." This sentence can be read like this:
For example:-12 and 13.412
Expressed as: -12 × 10-0 and 13412 × 10-3
Here (non-scaling values and scales) are represented as: [-12, 0] and [13412, 3] respectively
3. "If the scale value is a negative number, multiply the non-scale value of the number by a negative scale power of 10". This sentence can be read like this:
For example: 120.00
This value is expressed as: 12000 × 10-2
Here (non-scale value and scale) are expressed as: [12000, 2]
The value of the scale here is still positive 2, but perform the following operations:
BigDecimal amount = new BigDecimal("-120.00");
//Returns BigDecimal numerical equal to this decimal, but removes all tail zeros from this representation.
amount = amount.stripTrailingZeros();
This value is expressed as: 12 × 10-(-1)
Here (non-scale value and scale) are expressed as: [12, -1]
Notes on using
1. Constructor
The code copy is as follows: BigDecimal aDouble =new BigDecimal(1.22);
System.out.println("construct with a double value: " + aDouble);
BigDecimal aString = new BigDecimal("1.22");
System.out.println("construct with a String value: " + aString);
The output result is as follows:
construct with a doublevalue:1.219999999999999999999733546474089962430298328399658203125
construct with a String value: 1.22
Description of JDK:
a) The results of the construction method with parameter type double are unpredictable. Some people might think that the BigDecimal created by writing newBigDecimal(0.1) in Java is exactly equal to 0.1 (non-scaling value 1, whose scale is 1), but it is actually equal to 0.10000000000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be expressed accurately as a double (or for this case it cannot be expressed as any finite length binary decimal). In this way, the value passed into the construction method will not be exactly equal to 0.1 (although it is surface equal to that value).
b) On the other hand, the String constructor is completely predictable: writing to newBigDecimal("0.1") will create a BigDecimal, which is exactly the expected 0.1. Therefore, in comparison, it is generally recommended to use the String constructor first.
c) When the double must be used as a source for BigDecimal, note that this constructor provides an accurate conversion; it does not provide the same result as: first use the Double.toString(double) method, then use the BigDecimal(String) constructor. To convert double to String, you can also use String's static method: String.valueOf(double).
2. Operation. Addition, subtraction, multiplication and division actually return a new BigDecimal object, because BigDecimal is immutable, and a new object will be generated when performing each step of operation, so a.add(b); Although the addition operation is performed, a does not save the value after the addition operation. The correct usage should be a=a.add(b);
example:
Determine whether the BigDecimal object is an integer:
Copy the code as follows: private boolean isIntegerValue(BigDecimal bd) {
return bd.signum() == 0 || bd.scale() <= 0 || bd.stripTrailingZeros().scale() <= 0;
}
Why do this? Please test the following example:
Copy the code as follows: BigDecimal amount = new BigDecimal("-120.00");//Please try "0", "0.00", "1.00", "10.00" and "10.10" respectively
System.out.println(amount.signum());//Positive and negative
System.out.println(amount.scale()); //Scale
System.out.println(amount.stripTrailingZeros().scale());//Scale after zeroing
Reference: Class BigDecimal