Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A better than BigDecimal type for prices in Java?

Tags:

java

I need a financial type which will always keep numbers in X...X.YY format, even 20 digits, so I will get rid of rounding, formatting and other headache. Is there one?

Added: In other words I am looking for something like:

Financial f=1000.24;

f.setCurrency(USD);

System.out.print(f.toString()); -> $1'000.24


1 Answers

You can always keep financial data in longs, expressed in pennies. But, as you say, there are various headaches involved in doing that, and the max value is, I'm thinking, 19 digits. So BigDecimal would seem to be the way to go.

(Back when BitInteger/BigDecimal were being thought of I pushed for a packed decimal format that would have been lighter weight, but I couldn't get any support for it.)

like image 72
Hot Licks Avatar answered Feb 18 '26 10:02

Hot Licks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!