The floating point arithmetic has been a problem since long. That is why the usage BigDecimal
is encouraged. To your question on how to use it properly, I’ve a simple example which can help you understand how BigDecimal can be used along with MathContext.
BigDecimal bd1 = new BigDecimal(0.13); // double value to BigDecimal
BigDecimal bd2 = new BigDecimal("0.21"); // String value to BigDecimal
MathContext mc = new MathContext(3, RoundingMode.HALF_EVEN); // first arg is the precision and the second is the RoundingMode.
BigDecimal bd3 = bd1.add(bd2, mc); // perform the addition with the context
System.out.println(bd3); // prints 0.340
2
solved Is it compulsory to use BigDecimal for counting currency (Java)? [closed]