Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.5k points)
I have to calculate some floating point variables and my colleague suggest me to use BigDecimal instead of double since it will be more precise. But I want to know what it is and how to make most out of BigDecimal?

1 Answer

0 votes
by (46k points)

A BigDecimal is an accurate way of expressing numbers. A Double has a reliable accuracy. Going with doubles of various magnitudes (say d1=1000.0 and d2=0.001) could occur in the 0.001 being dropped collectively when summing as the variation in magnitude is so large. With BigDecimal this would not occur.

The problem of BigDecimal is that it's slower, and it's a bit more difficult to feed algorithms that way (due to + - * and / not being overloaded).

If you are trading with money, or accuracy is a must, use BigDecimal. Otherwise, Doubles tend to be good adequate.

I do suggest reading the Javadoc of BigDecimal as they do describe things better than I do here :)

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
0 votes
0 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...