First, Introduction
Java provides the API class BigDecimal in the Java.math package to perform precise operations on the number of more than 16 significant bits. Double-precision floating-point variable double can handle a 16-bit valid number. In practical applications, larger or smaller numbers need to be calculated and processed. float and double can only be used for scientific calculations or engineering calculations, and Java.math.BigDecimal are used in commercial calculations. BigDecimal creates objects, we cannot use traditional + 、-、 *,/et arithmetic operators to perform mathematical operations on their objects directly, but must call their corresponding methods. The arguments in the method must also be BigDecimal objects. Constructors are special methods of classes that are designed to create objects, especially objects with parameters.
Second, the constructor description
BigDecimal (int) Creates an object with the integer value specified by the parameter.
BigDecimal (double) creates an object with the double value specified by the parameter.
BigDecimal (long) creates an object with a long integer value specified by the parameter.
BigDecimal (String) creates an object that has the numeric value specified by the parameter as a string.
Iii. Description of the method
Add (BigDecimal) BigDecimal the value in the object, and then returns the object.
Subtract (BigDecimal) subtracts the value from the BigDecimal object and returns the object.
Multiply (BigDecimal) multiplies the values in the BigDecimal object and returns the object.
Divide (BigDecimal) divides the values in the BigDecimal object, and then returns the object.
ToString () Converts the numeric value of the BigDecimal object to a string.
Doublevalue () returns the value in the BigDecimal object as a double-precision number.
Floatvalue () returns the value in the BigDecimal object as a single-precision number.
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 control over currency values, hundred points, and general values that exceed 16 significant digits.
To take advantage of bigdecimal for the currency and percent formats as an example. First, the BigDecimal object is created, after the arithmetic operation of BigDecimal, a reference to the currency and percent formatting is established, and finally the formatted currency value and percentage are output using the BigDecimal object as the parameter of the format () method.
Public Static void Main(string[] args) {NumberFormat currency = numberformat.getcurrencyinstance ();//Establish currency formatting referenceNumberFormat percent = numberformat.getpercentinstance ();//Establish percent formatted referencePercent.setmaximumfractiondigits (3);//Percent decimal point up to 3 digitsBigDecimal Loanamount =NewBigDecimal ("15000.48");//Loan amountBigDecimal interestrate =NewBigDecimal ("0.008");//Interest rateBigDecimal interest = loanamount.multiply (interestrate);//MultiplySystem.out.println ("loan amount: \ t"+ Currency.format (loanamount)); System.out.println ("interest rate: \ t"+ Percent.format (interestrate)); System.out.println ("interest: \ T"+ Currency.format (interest)); }
The results show:
Loan Amount: ¥15,000.48
Interest Rate: 0.8%
Interest: ¥120.00
V. Comparison of BigDecimal
The BigDecimal is compared by using CompareTo (BigDecimal), as follows:
publicstaticvoidmain(String[] args) { new BigDecimal("1"); new BigDecimal("2"); new BigDecimal("1"); int result1 = a.compareTo(b); int result2 = a.compareTo(c); int result3 = b.compareTo(a); System.out.println(result1); System.out.println(result2); System.out.println(result3);}
The printing result is:-1, 0, 1, that is, the left is larger than the right, return 1, equal return 0, smaller than the right return-1.
Note You cannot use the Equals method to compare sizes.
The disadvantage of using BigDecimal is that performance is worse than double and float, especially when dealing with large, complex operations, depending on the actual need to decide which type to use.
V. Bigdecimal.setscale processing Java decimal point
BigDecimal.setScale()方法用于格式化小数点setScale(1)表示保留一位小数,默认用四舍五入方式 setScale(1,BigDecimal.ROUND_DOWN)直接删除多余的小数位,如2.35会变成2.3 setScale(1,BigDecimal.ROUND_UP)进位处理,2.35变成2.4 setScale(1,BigDecimal.ROUND_HALF_UP)四舍五入,2.35变成2.4setScaler(1,BigDecimal.ROUND_HALF_DOWN)四舍五入,2.35变成2.3,如果是5则向下舍
Comments:
1:scale refers to the number of digits after your decimal point. For example, 123.456 score is 3.
Score () is the method in the BigDecimal class. For example: BigDecimal B = new BigDecimal ("123.456");
B.scale (), which returns 3. The
2:roundingmode is a reserved mode for decimals. They are all constant fields in BigDecimal, and there are many kinds. BIGDECIMAL.ROUND_HALF_UP, for example, says 4 shekels and 5 in.
3:pubilc BigDecimal Divide (BigDecimal divisor, int scale, int roundingmode)
I used a BigDecimal object to divide the result after the divisor, and asked the result to retain a scale decimal place, roundingmode that is what the retention mode is, is rounding ah or other, you can choose!
4: The decimal digits for the general add, subtract, multiply methods are formatted as follows:
BigDecimal mData = new BigDecimal("9.655").setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println("mData=" + mData);
--Result:-–mdata=9.66
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
BigDecimal Usage Explanation