BigDecimal type in Java

1. When the definition field requires high precision, the BigDecimaltype is generally used .
2. The commonly used constructor
BigDecimal(int)creates an object
BigDecimal(double)with the integer value specified by the parameter. Creates an object with the double precision value specified by the parameter.
BigDecimal(long)Creates an object with the long integer value
BigDecimal(String)specified by the parameter. Creates an object with the value specified by the parameter in a string. Objects
can directly create BigDecimal objects and pass in corresponding values ​​of different types. However, when passing in float and double values, some unpredictable situations will occur.

double aa = 0.1116666;
BigDecimal bb = new BigDecimal(aa);
System.out.println("bb的值为"+bb);

The results are as follows:

bb的值为0.111666600000000004744293846670188941061496734619140625

Cause Analysis:

1) doubleThe result of the construction method with the parameter type is somewhat unpredictable. One might think that writing in Java newBigDecimal(0.1116666)created BigDecimalexactly equal 0.1116666, but it actually equals 0.111666600000000004744293846670188941061496734619140625. This is because it 0.1116666cannot be accurately expressed as double(or in this case, it cannot be expressed as any finite-length binary decimal). The value passed into the constructor will not be exactly equal 0.1116666(although on the surface it is equal to this value).

2) Stringconstruction method is completely predictable: writing newBigDecimal(“0.1116666”)will create a BigDecimal, which is exactly equal to the expected 0.1116666. Therefore, in comparison, it is generally recommended to use the Stringconstruction method first.

3) When a Doubletype d doubleturn into BigDeciamal, you can use the first Double.toString(double)method or String.valueOf(double)methods doubleconverted into Stringtype, and then call BigDeciamalparameter for Stringthe type of construction method, which is equivalent to the actual value.
3. BigDecimalDetailed explanation of common methods
1) Common methods
add(BigDecimal): BigDecimaladd the values ​​in the object to return the BigDecimalobject

subtract(BigDecimal): BigDecimalSubtract the values ​​in the BigDecimalobject and return the object

multiply(BigDecimal): BigDecimalMultiply the values ​​in the object to return the BigDecimalobject

divide(BigDecimal): BigDecimalDivide the value in the BigDecimalobject and return the object

toString(): Convert BigDecimalthe value in the object into a string

doubleValue(): Convert BigDecimalthe value in the object to a double precision number

floatValue(): Convert BigDecimalthe value in the object to a single precision number

longValue(): Convert BigDecimalthe value in the object to a long integer

intValue(): Convert BigDecimalthe value in the object to an integer
2), BigDecimalsize comparison
Java BigDecimalgenerally uses bigdemicalthe compareTomethod for comparing sizes

int a = bigdemical.compareTo(bigdemical2)

Return result analysis:

a = -1, means bigdemicalless than bigdemical2;
a = 0, means bigdemicalequal bigdemical2;
a = 1, means bigdemicalgreater than bigdemical2;
for example: a is greater than or equal to b

new bigdemica(a).compareTo(new bigdemical(b)) >= 0

4. BigDecima````格式化 由于NumberFormat 类的format() 方法可以使用BigDecimal 对象作为其参数,可以利用BigDecimal``` performs formatting control on currency values, percentage values, and general values ​​that exceed 16 significant digits.

Take the use BigDecimalof currency and percentage formatting as an example. First, create an BigDecimalobject, after performing BigDecimalarithmetic operations, respectively establish references to currency and percentage formatting, and finally use the BigDecimalobject as a format()method parameter to output its formatted currency value and percentage.

    NumberFormat currency = NumberFormat.getCurrencyInstance(); //建立货币格式化引用 
    NumberFormat percent = NumberFormat.getPercentInstance();  //建立百分比格式化引用 
    percent.setMaximumFractionDigits(3); //百分比小数点最多3位 
    
    BigDecimal loanAmount = new BigDecimal("15000.48"); //贷款金额
    BigDecimal interestRate = new BigDecimal("0.008"); //利率   
    BigDecimal interest = loanAmount.multiply(interestRate); //相乘
 
    System.out.println("贷款金额:\t" + currency.format(loanAmount)); 
    System.out.println("利率:\t" + percent.format(interestRate)); 
    System.out.println("利息:\t" + currency.format(interest)); 

result:

贷款金额: ¥15,000.48 利率: 0.8% 利息: ¥120.00

5. BigDecimalSummary
1). Use it when accurate decimal calculations are needed BigDecimal. BigDecimalThe performance ratio doubleand floatpoorness are especially obvious when dealing with huge and complex calculations. Therefore, the calculation of general accuracy is not necessary BigDecimal.
2). Try to use Stringthe constructor with parameter type .

Guess you like

Origin blog.csdn.net/weixin_43213064/article/details/109562365