6

I am trying to decide which data type shall i use for a financial application.

I have read that Double or BigDecimal should be used. And i am confused between them. Any help in this regard will be highly appreciated

Jabir
  • 2,776
  • 1
  • 22
  • 31

4 Answers4

9

You almost certainly don't want to use floating-point types (double, float, Double, Float) to handle monetary amounts, especially if you will be performing computations on them. The main reason for this is that there are many simple-looking numbers that cannot be represented exactly as a double et al. One such number is 0.1.

BigDecimal is therefore a much better choice for this use case.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

Use BigDecimal, it's a lot better than Double for financial stuff. See here the accepted answer for a similar question: Double vs. BigDecimal?

Community
  • 1
  • 1
user998692
  • 5,172
  • 7
  • 40
  • 63
2

BigDecimal isn't only best choice, BigDecimal is designed for such case.

1ac0
  • 2,875
  • 3
  • 33
  • 47
2

For most applications, a simple long will do: when representing cents, this will handle amounts up to 10^17 {Dollars, Euros, whatever}.

Gyro Gearless
  • 5,131
  • 3
  • 18
  • 14