3

I'm writing a financial application where i'm getting a value as string and need to check if the value is can be fit into double or BigDecimal without data loss.

What will be the best way to identify if the string value should be converted to a double or a BigDecimal?

1 Answers1

12

If it's a financial value, you should pretty much always use BigDecimal - or use an integer with an implicit scaling value (e.g. store cents instead of dollars).

Floating binary point (float, double) is almost never appropriate for financial applications, as the values usually have precise decimal values which aren't exactly representable in floating binary point.

That doesn't mean double is a useless data type - it's fine for natural, continuous values such as distances. It's just not good for "artificial" discrete values which are typically measured specified in decimal.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Jon's answer is correct. If you still want to do it, treat the value as Big Decimal and then convert it to double and then make sure that double and Big decimal are still the same. However don't get bothered use Big Decimal. – Geek Apr 30 '13 at 07:21