1. Introduction to BigDecimal
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 ).
2. Introduction of BigDecimal
When using Java programming languages such as banking and finance that require high-precision calculation of numerical values, we often use BigDecimal and BigInteger instead of the common int , long , float , and double types, especially when dealing with floating-point data.
Let's first take a look at a code demonstration of using the basic data type double for calculation and printing results:
public class MainClass { public static void main(String[] args) { System.out.println(0.02+0.01); System.out.println(0.05+0.01); }}The results are as follows:
0.030.0600000000000000000000005
The question is, why do data with the second result appear? The fundamental reason is that our computer is binary, and binary cannot accurately represent a floating point number. There are certain errors when the CPU uses the "mandrel and exponent" method (scientific notation method) to express floating point numbers. Therefore, when the data accuracy requirements are relatively high, the BigDecimal class still needs to be used, although the calculation speed is slightly slower.
3.BigDecimal use
There are two ways to create a BigDecimal object: constructor and public static method ( BigDecimal.valueOf ). Two points need to be paid attention to:
1. The constructor includes two forms that use basic data types and strings as parameters. The latter is recommended, such as: new BigDecimal(Double.valueOf(0.09)) . You can try it. The output result of System.out.println(new BigDecimal(0.06).toString()); statement is: 0.05999999999999999997779553950749686919152736663818359375
2. When Decimal prints the log or converts to basic data types, try to use the public method xxxValue() provided by it, such as doubleValue() , instead of a simple and crude toString()
4. BigDecimal rounding mode
Although the database stores a high-precision floating point number, it is usually necessary to limit the number of decimal digits when displayed in applications, such as two to three decimals. At this time, you need to use the setScale( int newScale, int roundingMode ) function. As a public static variable of BigDecimal, there are many operation rules for rounding mode (Rounding Mode) and there are eight public types. Here is an explanation, and the official document also introduces it.
1. ROUND_UP
Round away from zero. Abandon the non-zero part and add one number adjacent to the non-zero part.
2. ROUND_DOWN
Round in the direction close to zero. Abandon the non-zero part, and at the same time, the adjacent one number of the part will not be discarded by adding one to the non-zero part, and intercepting the behavior.
3. ROUND_CEILING
Round in the direction of infinite. If it is a positive number, the rounding result is the same as ROUND_UP; if it is a negative number, the rounding result is the same as ROUND_DOWN. Note: This mode does not reduce the value size.
4. ROUND_FLOOR
Round in the direction of negative infinite. If it is a positive number, the rounding result is the same as ROUND_DOWN; if it is a negative number, the rounding result is the same as ROUND_UP. Note: This mode does not increase the value size.
5. ROUND_HALF_UP
Rounding to the "closest" number is a rounding mode of upward rounding if the distance to two adjacent numbers is equal. If the part is discarded >= 0.5, the rounding behavior is the same as ROUND_UP; otherwise the rounding behavior is the same as ROUND_DOWN. This pattern is what we often call our "rounding".
6. ROUND_HALF_DOWN
Rounding to the "closest" number, if the distance to two adjacent numbers is equal, is a rounding mode of rounding down. If the discarded part > 0.5, the rounding behavior is the same as ROUND_UP; otherwise the rounding behavior is the same as ROUND_DOWN. This model is what we often call our "roundings".
7. ROUND_HALF_EVEN
Round to the "closest" number, if the distance to two adjacent numbers is equal, the adjacent even numbers are rounded. If the odd number on the left of the part is discarded, the rounding behavior is the same as ROUND_HALF_UP; if it is an even number, the rounding behavior is the same as ROUND_HALF_DOWN. Note: This rounding mode minimizes the accumulation error when repeated series of calculations. This rounding mode is also called the "banker rounding method" and is mainly used in the United States. Round six into six, and five into two situations. If the previous one is an odd number, it will enter the position, otherwise it will be abandoned.
8. ROUND_UNNECESSARY
The operation of the assertion request has accurate results, so rounding is not required. If this rounding mode is specified for an operation that obtains the exact result, an ArithmeticException is thrown.
Below, let’s give an example to illustrate the numerical calculation results in different rounding modes, keeping one decimal:
5. Summary
The above is the entire introduction of the BigDecimal and eight rounding modes of Java. I hope the content of this article will be helpful to everyone in learning Java.