Monetary Amounts in Java — Feedback Wanted

February 1, 2007 — Craig Jones

Last week I submitted two patches to the TimeAndMoney open source project on SourceForge (timeandmoney.domainlanguage.com/). One patch has to do with localization of money. The other patch has to do with the persistence of money, via Hibernate, to/from a database (with examples for both MS SQL, and Oracle).

Background: Our application deals with monetary amounts that have to be represented as different currencies, and do so from the perspective of different locales. (How we format US Dollar amounts here in the States is different than how Europeans format US Dollar amounts.) These are the issues we’ve identified:

  1. Maintaining fractions to the proper number of decimal places when doing the math.
  2. Maintaining the proper association between each monetary amount and its currency, as the amount passes through the system
  3. Determining what exchange rates to use when converting amounts between currencies.
  4. Properly localizing a monetary amount/currency combination when converting it to a string for display purposes.
  5. Properly parsing an entered string back into a monetary amount with respect to the local formatting customs.
  6. Persisting the monetary amount/currency combination as a single entity/atom.

There was no ready-made solution that covers all of these bases that we could find. The java.util.Currency type can be used to represent the currency code (en.wikipedia.org/wiki/ISO_4217, e.g. USD, EUR), but it does not represent any actual amount, nor the exchange rates. (One thing it does know is the number of decimal places associated with each currency.)One Money utility class we found (www.diasparsoftware.com/toolkit/apidocs/com/diasparsoftware/java/util/Money.html) was written by J.B.Rainsberger, the author of “JUnit Recipes” so it’s presumably quality code, but it is USD-specific. Our research also pointed us to Martin Fowler’s “Quantity” pattern. It combines an amount with a unit as described in his Analysis Patterns book (and online at www.martinfowler.com/ap2/quantity.html), and the main example of how to use it is for monetary amounts. He also has a Convertor pattern and a Money Bag pattern (book only). We would have referenced that if we had to start completely from scratch; however, …

We settled on using TimeAndMoney because, at least, it has the first two points above solidly covered. We had to add our own support for localization, though. Also, although there was an existing side project for Hibernate support (plus one patch), it didn’t take advantage of the User Type construct in Hibernate (for Money), so we had implement that as well. These are the patches mentioned above. My team is anxious for feedback.

BTW, we haven’t tackled the exchange rate conversions yet. I don’t ever expect to find a prepackaged solution for that, since that’s mostly a matter of subscribing to a service that provides the current rates and then just performing the arithmetic. Of course, the application will somehow have to determine when and how to lock in the rates over time, but that’s a business rules problem, not a money representation problem.

Tags: ,

Leave a Reply

Twitter Updates

  • On a mac it's super easy to encrypt/decrypt a file using openssl 2009-12-02
  • If you don’t think carefully, you might think that programming is just typing statements in a programming language. — Ward Cunningham 2009-12-01
  • Thanks to everyone at my CodeCamp LA presentation, you had great questions and I hope the exercise gave you something to take to work Monday 2009-11-21
  • Turns out my CodeCamp LA presentation made it to the front page of SlideShare today. Must be a slow day. 2009-11-21
  • Scrum has two deliverables, A Product Deliverable and a Capability Deliverable we don't talk up the second enough as agilists 2009-11-21
  • More updates...


Bad Behavior has blocked 59 access attempts in the last 7 days.