**by BehindJava**

# Float, Double and BigDecimal with Financial Calculation

In this tutorial we are going to learn in detail about Float, Double and BigDecimal with Financial Calculation.

This is one of common mistake Java programmer make until they are familiar with BigDecimal class. When we learn Java programming we have been told that use float and double to represent decimal numbers its not been told that result of floating point number is not exact, which makes them unsuitable for any financial calculation which requires exact result and not approximation.

Float and double are designed for engineering and scientific calculation and many times doesn’t produce exact result also result of floating point calculation may vary from JVM to JVM. Look at below example of BigDecimal and double primitive which is used to represent money value, its quite clear that floating point calculation may not be exact and one should use BigDecimal for financial calculations.

```
public class BigDecimalExample {
public static void main(String args[]) throws IOException {
//floating point calculation
double amount1 = 2.15;
double amount2 = 1.10;
System.out.println("difference between 2.15 and 1.10 using double is: " + (amount1 - amount2));
//Use BigDecimal for financial calculation
BigDecimal amount3 = new BigDecimal("2.15");
BigDecimal amount4 = new BigDecimal("1.10") ;
System.out.println("difference between 2.15 and 1.10 using BigDecimal is: " + (amount3.subtract(amount4)));
}
}
Output:
difference between 2.15 and 1.10 using double is: 1.0499999999999998
difference between 2.15 and 1.10 using BigDecmial is: 1.05
```

Another mistake Java Programmers make is using wrong constructor of BigDecmial. BigDecimal has overloaded constructor and if you use the one which accept double as argument you will get same result as you do while operating with double. So always use BigDecimal with String constructor. here is an example of using BigDecmial constructed with double values:

```
//Creating BigDecimal from double values
BigDecimal amount3 = new BigDecimal(2.15);
BigDecimal amount4 = new BigDecimal(1.10) ;
System.out.println("difference between 2.15 and 1.0 using BigDecmial is: " + (amount3.subtract(amount4)));
Output:
difference between 2.15 and 1.10 using double is: 1.0499999999999998
difference between 2.15 and 1.10 using BigDecmial is: 1.049999999999999822364316059974953532218933105468750
```

One more mistake from Java programmer can be using result of floating point calculation for determining conditions on loop. Though this may work some time it may result in infinite loop another time. See below example where your Java program will get locked inside infinite while loop:

```
double amount1 = 2.15;
double amount2 = 1.10;
while((amount1 - amount2) != 1.05){
System.out.println("We are stuck in infinite loop due to comparing with floating point numbers");
}
Output:
We are stuck in infinite loop due to comparing with floating point numbers
We are stuck in infinite loop due to comparing with floating point numbers
……………
…………..
```

This code will result in infinite loop because result of subtraction of amount1 and amount 2 will not be 1.5 instead it would be “1.0499999999999998” which make boolean condition true.

- Don’t use float and double on monetary calculation.
- Use BigDecimal, long or int for monetary calculation.
- Use BigDecimal with String constructor and avoid double one.
- Don’t use floating point result for comparing loop conditions.