Search code examples
javadesign-patternsmodelmodeling

How to model an amount of Money in Java


Can anybody recommend a class library or a design pattern that models amounts of Money?

I guess it should support:

  • multiple currencies
  • a mechanism to indicate the number of decimals
  • math (respecting certain rounding conventions (cfr ie. Banker's Rounding))
  • serialisation to and from a String representation
  • ... ?

I.e. 19.99 USD could be serialized into "USD-2-00000001999"
(Whereas 2 indicates the number of decimals)


Solution

  • I would check the monetary module from the JScience project (by Jean Marie Dautelle).

    Depending on your exact needs, Stephen Colebourne started Joda-Money ("a more focussed project" [than JScience]) some time ago. But there is no full release available yet (version 0.5 was released one year ago).