# Use BigDecimal to perform exact operations of of for addition subtraction multiplication and division

• 2020-04-01 02:33:41
• OfStack

``````
public class Test_1 {
public static void main(String[] args) {
System.out.println(0.06+0.01);
System.out.println(1.0-0.42);
System.out.println(4.015*100);
System.out.println(303.1/1000);
}
}
``````

The results are as follows
0.06999999999999999
0.5800000000000001
401.49999999999994
0.30310000000000004

You think you're wrong, but it turns out this way. What's the problem? The reason is that our computers are binary. Floating point Numbers have no way of being accurately represented in binary. Our CPU representation of floating point Numbers consists of two parts: the exponent and the mantisse. Such a representation usually loses some degree of accuracy, and some floating point calculations also produce some errors. For example, the binary representation of 2.4 is not exactly 2.4. Instead, the closest binary representation is 2.39999999999999. The value of a floating point number is actually calculated by a particular mathematical formula.
In fact, Java floats can only be used for scientific or engineering calculations. In most commercial calculations, the java.math.bigdecimal class is used for precise calculations.
When calculating using the BigDecimal class, the following steps are used:
1. Build BigDecimal objects with float or double variables.
2. Arithmetic operations are performed by calling the corresponding methods of BigDecimal, such as addition, subtraction, multiplication, and division.
3. Convert BigDecimal objects to float, double, int, etc.
In general, variables of basic types can be built into BigDecimal objects using either the BigDecimal constructor or the valueOf() method for static methods.

``````
BigDecimal b1 = new BigDecimal(Double.toString(0.48));
BigDecimal b2 = BigDecimal.valueOf(0.48);
``````

The BigDecimal class provides member methods for the usual addition, subtraction, multiplication, and division.

``````
public BigDecimal subtract(BigDecimal value); //subtraction
public BigDecimal multiply(BigDecimal value); //The multiplication
public BigDecimal divide(BigDecimal value); //division
``````

After the corresponding calculation, we may need to convert the BigDecimal object into a variable of the corresponding basic data type, using methods like floatValue(), doubleValue(), etc.
Below is a utility class that provides addition, subtraction, multiplication, and division operations.

``````
public class Arith {

public static double add(double value1,double value2){
BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
}

public static double sub(double value1,double value2){
BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
return b1.subtract(b2).doubleValue();
}

public static double mul(double value1,double value2){
BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
return b1.multiply(b2).doubleValue();
}

public static double div(double value1,double value2,int scale) throws IllegalAccessException{
//If the exact range is less than 0, an exception message is thrown
if(scale<0){
throw new IllegalAccessException(" Accuracy not less than 0");
}
BigDecimal b1 = new BigDecimal(Double.valueOf(value1));
BigDecimal b2 = new BigDecimal(Double.valueOf(value2));
return b1.divide(b2, scale).doubleValue();
}
}
``````

Related articles: