When doing business calculations in Java, especially when it comes to currencies, you would preferably use the_ java.math.BigDecimal_ class to avoid the problems related to floating-point arithmetic, which you might experience if using one of the two primitive types float or double (or one of their boxed type counterparts).
Indeed, the _BigDecimal _class contains a number of methods that can meet most of the requirements of common business calculations.
However, I would like to draw your attention to the most common pitfalls in the use of the _BigDecimal _class and show you how to avoid them, by using the regular _BigDecimal _API on the one hand and a new, customized class that extends _BigDecimal _on the other.
So, let’s start with the regular _BigDecimal _API.
Consider the following example:
BigDecimal x = new BigDecimal(0.1);
System.out.println("x=" + x);
Console output: x=0.1000000000000000055511151231257827021181583404541015625
#programming #java #coding #pitfalls #software-development