# BigDecimal precise calculation tool class

Posted by vladj on Mon, 10 Feb 2020 14:06:47 +0100

# Preface

In the actual development, when encountering commercial calculation such as currency and statistics, the java.math.BigDecimal class is generally used for accurate calculation. And this kind of operation is usually predictable, that is, universal. So, write a tool class to facilitate future work.

# Construction of BigDecimal

Generally speaking, we mainly calculate from int, long, double and float, which is recommended for construction

`BigDecimal BigDecimal(String s);`

Because precision is lost through double construction, and String construction is a fixed value.
Create the following as a generic BigDecimal converter:

```    /**
* Number -> BigDecimal
*/
public static <T extends Number> BigDecimal transform(T v) {
if (v instanceof Double) {
return new BigDecimal(Double.toString((Double) v));
} else if (v instanceof Integer) {
return new BigDecimal(Integer.toString((Integer) v));
} else if (v instanceof Long) {
return new BigDecimal(Long.toString((Long) v));
} else if (v instanceof Short) {
return new BigDecimal(Short.toString((Short) v));
} else if (v instanceof Float) {
return new BigDecimal(Float.toString((Float) v));
} else {
return (BigDecimal) v;
}
}```

# BigDecimal method

There are four calculation types: addition, subtraction, multiplication and division. The methods provided by BigDecimal are also designed around these four calculation types.

```BigDecimal add(BigDecimal augend) //plus
BigDecimal subtract(BigDecimal subtrahend) //reduce
BigDecimal multiply(BigDecimal multiplicand) //ride
BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) //except```

Based on addition, subtraction, multiplication and division, tool class provides

1. Chain computing, similar to JDK8 lamada api, is a refreshing and silky programming experience
2. Support sum and average of sets
3. Support composite computing, such as 2 * (2 + 8)

# Practical case of BigDecimal precise calculation tool class

## Convert precision to BigDecimal, do not specify precision

```System.out.println(PreciseCalculations.transform(121.11)); //Convert double - > 121.11
System.out.println(PreciseCalculations.transform(Integer.MAX_VALUE)); //Convert int - > 2147483647
System.out.println(PreciseCalculations.transform(Short.MAX_VALUE)); //Convert short - > 32767
System.out.println(PreciseCalculations.transform(Long.MAX_VALUE)); //Convert long - > 9223372036854775807
System.out.println(PreciseCalculations.transform(121.19F)); //Convert float - > 121.19```

## Convert precision to BigDecimal, specify precision

```System.out.println(PreciseCalculations.transform(121.1111111111, 5)); //Precision greater than specified - > 121.11111
System.out.println(PreciseCalculations.transform(121.11, 5)); //If the precision is less than the specified precision, make-up zero - > 121.11000```

## Add, subtract, multiply and divide

```System.out.println(PreciseCalculations.add(12.11, 12.11)); //Add - > 24.22
System.out.println(PreciseCalculations.subtract(12.11, 12.11)); //Subtraction - > 0.00
System.out.println(PreciseCalculations.multiply(12.11, 12.11)); //Multiply - > 146.6521
System.out.println(PreciseCalculations.divide(12.11, 2.35, 5)); //Division - > 5.15319```

## Negative number calculation

```// -1.11 * 13 - 90 = -104.43
// -11.11111111 + 90 = 78.88888889

## Sum and average of sets

```List<Double> list = Arrays.asList(12.11D, 13.11D, 14.11D, 15.321312D);
System.out.println(PreciseCalculations.sum(list)); //Sum - > optional [54.651312]
System.out.println(PreciseCalculations.average(list)); //Average - > optional [13.66283]
System.out.println(PreciseCalculations.average(Collections.emptyList())); //Empty set - > optional.empty```

## Composite computation

```// Calculation 121.11 * 13 / 60 + 100 - 12 = 114.24050