I need a financial type which will always keep numbers in X...X.YY format, even 20 digits, s开发者_JAVA技巧o 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
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.)
There is no standard Java type like that.
A BigInteger (in pennies) is probably the best choice out of the built-in types. (You don't need to worry about rounding that way). You can always make your own by wrapping BigInteger for convenience to add things like the currency marker and output in dollars.
精彩评论