Java Mistake 1 - Using float and double for monetary or financial calculation Example (original) (raw)

Java is considered a very safe programming language compared to C and C++ as it doesn't have free() and malloc() to directly do memory allocation and deallocation, You don't need to worry about array overrun in Java as they are bounded and there is NO pointer arithmetic in Java. Still, there are some sharp edges in the Java programming language that you need to be aware of while writing enterprise applications. Many of us make a subtle mistake in Java which looks correct in the first place but turns out to be buggy when looked at carefully. In this series of java articles, I will be sharing some common Java mistakes programmers make while programming applications in Java.

As I have said earlier every day we learn new things but we forget something equally important. This again highlights the importance of code review and the following best practices in Java. In this part, we will discuss why double and float should not be used in the monetary or financial calculation where the exact result of the calculation is expected.

And, If you are new to Java then I also recommend you go through these Java Programming online courses to learn Java programming and developmentin a better and more structured way. This is one of the best and up-to-date courses to learn Java online.

Using double and float for exact calculation

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

Both float and double are designed for engineering and scientific calculation and many times don’t produce exact results also result of the floating-point calculation may vary from JVM to JVM. Look at below example of BigDecimal and double primitive which is used to represent monetary value, It 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.0 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.0 using BigDecimal is: " + (amount3.subtract(amount4)));

}

}

Output:

difference between 2.15 and 1.0 using double is: 1.0499999999999998

difference between 2.15 and 1.0 using BigDecmial is: 1.05

From above example of floating point calculation is pretty clear that result of floating point calculation may not be exact at all time and it should not be used in places where exact result is expected.

Using Incorrect BigDecimal constructor

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.0 using double is: 1.0499999999999998

difference between 2.15 and 1.0 using BigDecmial is: 1.049999999999999822364316059974953532218933105468750

I agree there is not much difference between these two constructor but you got to remember this.

Using result of floating point calculation in loop condition

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 an infinite loop due to comparing with floating-point numbers

We are stuck in an infinite loop due to comparing with floating-point numbers

……………

…………..

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

That’s all on this part of learning from mistakes in Java, bottom line is :

Other Java tutorials you may like