Java programming BigDecimal usage example sharing
Java provides operation classes with large numbers (more than 16 significant bits), that is, Java math. Bininteger class and Java math. BigDecimal class, used for high-precision calculation BigInteger class is the processing class for large integers, and BigDecimal class is the processing class for size numbers
BigInteger class is used in the implementation of BigDecimal class. The difference is that BigDecimal adds the concept of decimal
Float and double can only be used for scientific calculation or engineering calculation; In business calculation, the number accuracy is required. BigInteger and BigDecimal classes must be used. It supports fixed-point numbers with any precision and can be used to accurately calculate currency values
BigDecimal class creates objects. Instead of using traditional arithmetic operators such as +, -, *, /, it must call its corresponding methods The parameter of the method must also be an object of type BigDecimal
The following methods are commonly used to construct BigDecimal objects:
BigDecimal BigDecimal(double d); // BigDecimal (string s) is not allowed// Commonly used, static BigDecimal valueof (double D) is recommended// Commonly used, recommended
Among them,
1. Construction method of double parameter, not allowed!!!! Because it can't get the corresponding value accurately; 2. The string construction method is completely predictable: writing new BigDecimal ("0.1") will create a BigDecimal, which is exactly equal to the expected 0.1; Therefore, it is generally recommended to give priority to the string construction method; 3. The static method valueof (double VAL) is implemented internally, and the double type is still changed to string type; This is usually the preferred method to convert double (or float) into BigDecimal;
The test code is as follows:
The output is as follows:
Attachment 1: source code of valueof () method of BigDecimal class
Attachment 2: several common methods of BigDecimal class
Annex 3 provides the tool source code of accurate floating-point operation (including addition, subtraction, multiplication, division and rounding)
Next, summarize the usage of BigDecimal in this project.
1. Addition, subtraction, multiplication and division 2 Setting accuracy 3 Reverse
Addition, subtraction, multiplication and division call the function separately
give an example:
Precision setting, why is the precision set? Let's see an effect
The code is as follows
The results are as follows:
It's not the 0.9 and 1.1 we want to see. The reason is that there will be accuracy problems when converting to binary, resulting in such results. Therefore, we can add precision during operation, or use string when instantiating BigDecimal.
How to set precision:
In this way, you can set the two digit accuracy
String instantiation method:
Reverse
Because BigDecimal cannot be calculated directly with + - * / symbols, a separate method is also needed to achieve inversion:
So you get the opposite number:
These are the points used in this project
summary
The above is all the content shared in this article about the usage examples of BigDecimal in Java programming. I hope it will be helpful to you. Welcome to: detailed explanation of Dao mode and code examples of Java, detailed explanation of automatic unpacking and automatic packing in Java programming, detailed explanation of Java array foundation, etc. if you have any questions, you can leave a message at any time. You are welcome to point out!