jGuru Forums

BigDecimal Vs Double
3 posts in topic
Flat View
TOPIC ACTIONS:

Posted By:   amit_gupta
Posted On:   Wednesday, June 6, 2001 02:52 AM

In the recent topic in the JavaWorld, Robert Nielsen discusses the use of Big Decimal for currencies and finance areas. I wanna ask why BigDecimal, why not double?. Because while formatting the currency for a particualr locale, we use java.text.NumberFormat's format() method which accept double argument. So why to use first Bigdecimal and then convert to double?. basically i want to know the advantages of BigDecimal over double.

Re: BigDecimal Vs Double

Posted By:   John_Mitchell
Posted On:   Sunday, June 17, 2001 03:38 PM

Precision and accuracy. `double` uses a fixed-size data format so it can't always be precise or accurate in its representation of numbers. On the other hand, the `BigDecimal` class will use as much space as is necessary and uses computational algorithms that will be both precise and accurate for any number.

If you're dealing with any sort of numerical use where spot on precision and accuracy are critical then you really need to use BigDecimal rather than double.

Re: BigDecimal Vs Double

Posted By:   Michael_Wax
Posted On:   Wednesday, June 6, 2001 09:14 AM

If you are displaying a single value, using double and NumberFormat.format will work. However, for any sort of calculations, using double will result in deviations from the correct results fairly quickly. I have seen the use of double result in discrepancies of a penny or two on actual commercial calculations. While this may seem small (after all, a penny typically corresponds to less than 0.1% of the total value), it is unacceptable in for commercial and financial transactions. If you are building a system for real-world use, you will need to use BigDecimal.

Re: BigDecimal Vs Double

Posted By:   Michael_Wax
Posted On:   Wednesday, June 6, 2001 08:34 AM

If you are displaying a single number, using double with NumberFormat is fine. If you are doing calculations, you will see discrepancies from the actual values very quickly using double and NumberFormat. While the discrepancies may be small (much less than 1%), financial calculations must have absolute accuracy. After all, I know that I would be unhappy if my bank account were off by a few pennies every month. For any real-world application involving money, you need to use BigDecimal.