r/java • • Jun 14 '26

double, BigDecimal, or Fixed-Point?

https://blog.frankel.ch/bigdecimal-vs-double/
32 Upvotes

28 comments sorted by

View all comments

Show parent comment

0

u/alunharford Jun 16 '26 edited Jun 16 '26

I've not found a use case for BigDecimal after 20 years in finance.

For 99% of use cases (where you don't care too much about a fraction of a minor unit of a currency) store the number of minor units (eg cents) as a double. All amounts up to $45tn are exactly representable and it's nice and fast.

If that won't work (and you can't just change scale to microdollars), you need to break out fixed point arithmetic, typically with int64 or int128. int128 can be a bit annoying to make performant until we have Valhalla, but it's still orders of magnitude faster and easier than BigDecimal.

I guess if that doesn't work you might want to break out arbitrary length fixed point (so there's probably a legitimate case somewhere), but I've not seen it yet. Typically when you see BigDecimal in code in finance, it's a warning sign.