As a Java
developer in the daily work, many times we will encounter some need to calculate the data scenario, usually for the scenario does not need to calculate the precision we can use Integer
, Float
or Double
to calculate, although will lose precision but occasionally can be used, if we need to calculate the results accurately, we will use the class BigDecimal provided in the java.math package to implement the corresponding function.
BigDecimal
is a tool for accurate data calculation, since it is a data calculation, it will definitely provide the corresponding methods of addition, subtraction, multiplication and division for us to use, as follows.
add(BigDecimal)
: Add the values in theBigDecimal
object and return theBigDecimal
objectsubtract(BigDecimal)
: The values inBigDecimal
object are subtracted, andBigDecimal
object is returned.multiply(BigDecimal)
: Multiply the values in theBigDecimal
object and return theBigDecimal
objectdivide(BigDecimal)
: Divide the values in theBigDecimal
object, and return theBigDecimal
object.
When we need to use the corresponding method, we need to create BigDecimal
object first, and then we can use it, and the corresponding constructor methods are
BigDecimal(int)
: Create an object with the integer value specified by the parameterBigDecimal(double)
: Creates an object with the double precision value specified by the parameterBigDecimal(long)
: Creates an object with the long integer value specified by the parameterBigDecimal(String)
: Creates an object with the value specified by the parameter as a string.
After the BigDecimal
object is created by the constructor method, the corresponding method is called and another BigDecimal
parameter is passed to achieve the corresponding addition, subtraction, multiplication and division method. The following example is shown.


The result of the run is shown below.
From the above figure, we can see that BigDecimal is used in a specific way by calling the corresponding method and passing in the corresponding parameters. However, when dividing, we can see that the divide method also provides the parameter to set the exact number of digits, and it is possible to set the specific rounding method. There are several integer methods.


In addition to the methods of addition, subtraction, multiplication and division, BigDecimal also provides the method compareTo() for comparing two BigDecals, the usage is as follows
Because num2 is larger than num1, the return value is 1; when num2 is equal to num1, it returns 0; when num2 is smaller than num1, it returns 1.
The summary format is as follows
if (num2.compareTo(num1) > 0)
judgment is a common operation.
Also BigDecimal provides methods to convert directly to int, long, float, double numeric values as follows, and it is used relatively rarely in general.
Use BigDecimal
when you need accurate decimal calculation in your daily work, the performance of BigDecimal
is relatively worse than double
and float
, so use it only when you need it. BigDecimal
creates a new object every time we add, subtract, multiply and divide, and we need to save it when we need to use it later, so usually we try to use the constructor of String type.