Yes, you must change all floats or doubles to take either ints, longs, or BigDecimals .
Floats and doubles are not accurate for financial calculations. It is a very good idea to use the Money template to process amounts and currencies (this is a special type of Quantity ). To maintain a list of funds, possibly in different currencies, what you do efficiently is MoneyBag , a Money collection that can then summarize all values ββbased on the target currency and CurrencyExchangeService (currency conversion rates should also be saved as BigDecimals ).
Rounding should be performed after each operation in accordance with the number of decimal places desired and the rounding algorithm. The number of decimal places is usually a Currency property (see, for example, ISO 4217 ); if another number is not required (for example, when calculating gasoline, for example).
You should definitely look at Fowler's examples; but I also created a very simple uni-currency Money for the exercise . It uses only dollars and rounds up to two decimal places; but it is still a good base for future extensions.
JordΓ£o
source share