1. When the definition field requires high precision, the BigDecimal
type 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) double
The result of the construction method with the parameter type is somewhat unpredictable. One might think that writing in Java newBigDecimal(0.1116666)
created BigDecimal
exactly equal 0.1116666
, but it actually equals 0.111666600000000004744293846670188941061496734619140625
. This is because it 0.1116666
cannot 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) String
construction 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 String
construction method first.
3) When a Double
type d double
turn into BigDeciamal
, you can use the first Double.toString(double)
method or String.valueOf(double)
methods double
converted into String
type, and then call BigDeciamal
parameter for String
the type of construction method, which is equivalent to the actual value.
3. BigDecimal
Detailed explanation of common methods
1) Common methods
add(BigDecimal)
: BigDecimal
add the values in the object to return the BigDecimal
object
subtract(BigDecimal)
: BigDecimal
Subtract the values in the BigDecimal
object and return the object
multiply(BigDecimal)
: BigDecimal
Multiply the values in the object to return the BigDecimal
object
divide(BigDecimal)
: BigDecimal
Divide the value in the BigDecimal
object and return the object
toString()
: Convert BigDecimal
the value in the object into a string
doubleValue()
: Convert BigDecimal
the value in the object to a double precision number
floatValue()
: Convert BigDecimal
the value in the object to a single precision number
longValue()
: Convert BigDecimal
the value in the object to a long integer
intValue()
: Convert BigDecimal
the value in the object to an integer
2), BigDecimal
size comparison
Java BigDecimal
generally uses bigdemical
the compareTo
method for comparing sizes
int a = bigdemical.compareTo(bigdemical2)
Return result analysis:
a = -1, means bigdemical
less than bigdemical2
;
a = 0, means bigdemical
equal bigdemical2
;
a = 1, means bigdemical
greater 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 BigDecimal
of currency and percentage formatting as an example. First, create an BigDecimal
object, after performing BigDecimal
arithmetic operations, respectively establish references to currency and percentage formatting, and finally use the BigDecimal
object 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. BigDecimal
Summary
1). Use it when accurate decimal calculations are needed BigDecimal
. BigDecimal
The performance ratio double
and float
poorness are especially obvious when dealing with huge and complex calculations. Therefore, the calculation of general accuracy is not necessary BigDecimal
.
2). Try to use String
the constructor with parameter type .