Sahil Sharma wrote:Yeah, now i am using the DecimalFormat to format the result and it giving me what i want.
Thanks guys for all the help.
When formatting at the end, wonder what kind of rounding (RoundingMode) OP used or others suggest to use here. Because using different rounding methods and scale, together, would return different results, anyways. What I meant to say is that this calculation might have matched the result from using double data types and formatting at the end with a specific rounding might not match for another calculation. In complex and lengthier or looped calculations, the errors just propagate.
Whereas BigDecimal pretty much matches the results with those from the calculator. Here is an example quoted from the web, that can be solved by both BigDecimal and formatting.
The above problem can be solved by using BigDecimal as follows:
But because calculations are a pain and performance worse you could always do something like this:
which will also give the result same as the BigDecimal.
Not only is the choice of data type tricky but choice of roundingMode and scale are also tricky to just get calculator kind calculations. Could not come across any recently written articles when I googled about the best practices in using BigDecimal, when to use and how to use for optimal performance, any suggestions?