BigDecimal usage and bigdecimal usage
The API class BigDecimal provided by Java in the java. math package is used to accurately calculate the number of valid 16 bits. Double-precision floating-point variable can process 16-bit valid numbers. In practical applications, You need to calculate and process larger or smaller numbers. Float and double can only be used for scientific or engineering computing. java. math. BigDecimal is used in commercial computing. BigDecimal creates an object. We cannot use traditional arithmetic operators such as +,-, *, And/to perform mathematical operations on the object directly. Instead, we must call the corresponding method. The parameters in the method must also be BigDecimal objects. The constructor is a special method of a class that is used to create objects, especially objects with parameters.
- 2. constructor description
BigDecimal (int) creates an object with the integer specified by the parameter.
BigDecimal (double) creates an object with the double precision value specified by the parameter.
BigDecimal (long) creates an object with the Length Integer specified by the parameter.
BigDecimal (String) creates an object with the value specified by the parameter in the String.
Add (BigDecimal) values in the BigDecimal object are added, and then this object is returned.
Subtract (BigDecimal) values in the BigDecimal object subtract, and then return this object.
Multiply the values in the multiply (BigDecimal) BigDecimal object and return this object.
Divide (BigDecimal) values in the BigDecimal object are divided, and then this object is returned.
ToString () converts the value of the BigDecimal object to a string.
DoubleValue () returns the values in the BigDecimal object with double precision.
FloatValue () returns the values in the BigDecimal object with a single precision.
LongValue () returns the value in the BigDecimal object as a long integer.
IntValue () returns the value in the BigDecimal object as an integer.
- Iv. formatting and Examples
Because the format () method of the NumberFormat class can use the BigDecimal object as its parameter, you can use BigDecimal to format and control the monetary value, the hundred value, and the general value of a valid number that exceeds 16 bits.
This example uses the BigDecimal pair to format the currency and percentage. First, create a BigDecimal object, perform arithmetic operations on the BigDecimal, create reference to the formatting of the currency and percentage, and use the BigDecimal object as the parameter of the format () method, output the formatted currency value and percentage.
1 public static void main (String [] args) {2 NumberFormat currency = NumberFormat. getCurrencyInstance (); // create a currency format Reference 3 NumberFormat percent = NumberFormat. getPercentInstance (); // create a percentage format reference 4 percent. setMaximumFractionDigits (3); // percentage of decimal places up to 3 digits 5 6 BigDecimal loanAmount = new BigDecimal ("15000.48 "); // loan amount 7 BigDecimal interestRate = new BigDecimal ("0.008"); // interest rate 8 BigDecimal interest = loanAmount. multiply (interestRate); // multiply by 9 10 System. out. println ("loan amount: \ t" + currency. format (loanAmount); 11 System. out. println ("Interest Rate: \ t" + percent. format (interestRate); 12 System. out. println ("Interest: \ t" + currency. format (interest); 13}
The running result is as follows:
Loan Amount: ¥15,000.48 interest rate: 0.8% interest: ¥120.00
BigDecimal is compared by using compareTo (BigDecimal). The specific comparison is as follows:
1 public static void main(String[] args) { 2 BigDecimal a = new BigDecimal("1"); 3 BigDecimal b = new BigDecimal("2"); 4 BigDecimal c = new BigDecimal("1"); 5 int result1 = a.compareTo(b); 6 int result2 = a.compareTo(c); 7 int result3 = b.compareTo(a); 8 System.out.println(result1); 9 System.out.println(result2);10 System.out.println(result3);11 12 }
The output is-1, 0, and 1. That is, if the left side is greater than the right side, 1 is returned. If the left side is equal, 0 is returned. If the left side is smaller than the right side,-1 is returned.
Note that the equals method cannot be used to compare the size.
The disadvantage of using BigDecimal is that its performance is worse than that of double and float. It is especially obvious when processing large and complex operations, because the type is determined based on actual needs.