Java Bigdecimal type operation, javabigdecimal
Bigdecimal type operation in Java
Double-precision floating-point variable can process 16-bit valid numbers. In practical applications, You need to calculate and process larger or smaller numbers. The API class BigDecimal provided by Java in the java. math package is used to accurately calculate the number of valid 16 bits. Table 5.7 lists the main constructors and methods of the BigDecimal class.
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.
Method description
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.
Note: Generally, because the numeric type, such as double, cannot accurately represent a number greater than the 16-bit valid number, it makes sense to use the BigDecimal (String) constructor to create an object. In addition, BigDecimal creates an object. We cannot directly perform mathematical operations on the object using traditional arithmetic operators such as +,-, *, And, instead, the corresponding method must be called. The parameters in the method must also be BigDecimal objects.
Eg:
Multiply two BigDecimal data types:
BigDecimal a = newbigdecimal (15124 );
BigDecimal B = new BigDecimal (15124 );
BigDecimal c = a. multiply (B );