Skip to content

Money Is Money v 0.14

I’ve made a modest – very modest – library for dealing with Monetary amounts. IMHO something like this should be inside Java, to stop all those idiots using floating point logic to calculate money amounts. My library uses exactly NO EXTERNAL DEPENDENCIES apart from what’s already in Java 5 and Junit 3.8.1 for tests, which were by and large, all written first. It’s release with the MIT Licence, so you can use it freely as long as you include the copyright notices.

It’s still a work in progress. You can check it out with SVN from http://crazymcphee.net/svn/money/trunk/

From the README:

This package is copyright 2008-2009 Scot Mcphee.

Licence is the M.I.T. Licence. Please see the file “LICENCE”.

WHAT IS Money-Is-Money?

Money-Is-Money is a library for the correct manipulation of monetary data. I wrote this small library because I found that there were not any available public source Money implementations, despite some Time-based libraries promising such (I guess Time is more interesting to programmers than filthy lucre).

It consists primarily of an interface, called ‘Money’, and a Factory for making money, called ‘MoneyMaker’.  There are a number of implementation classes, primarily CashMoney and  PrecisionMoney. They both share a abstract implementation, AbstractMoney. For the time being. I can see ways to eliminating both and just having a single implementation of the Money interface, but I haven’t done it yet.

However, for the time being, CashMoney is used whenever MoneyMaker is given a Currency (that’s java.util.Currency)  which has a default scale less than the given BigDecimal’s scale. Yes, that’s a java.math.BigDecimal. If you whine  about wanting to use doubles and floats congratulations! you are the target audience and therefore you should *really* be using this library and I won’t go into debate here why it’s wrong to use floating point logic in  Money
implementations.

Money is designed to be an immutable object. All operations on Money, such as Money.add(Money) returns a new instance  of Money representing the added amounts. There are operations for add, subtract, divide, and multiply. The default rounding mode is HALF_EVEN or ‘bankers rounding’. If a value comes out to 0.5, it is rounded to the EVEN value,  so $3.555 at  a precision of 2 decimal places becomes 3.56, and so does $3.565. Divide and multiply also include methods to perform those operations with a specified rounding mode.

There is also a method ‘proRate’. This takes the existing money and divides it up into the number of buckets you  specify, with the modulo distributed in as small as increments as possible across as money of the buckets as possible. This is NOT THE SAME as dividing by the number you specify. So $10.00 divided into 3 buckets results in buckets containing $3.34, $3.33, $3.33. The specification is, adding the buckets back up should always result in the original amount without any rounding errors. You should be able to pro rate an amount, then pro rate those amounts, recursively many many times and adding up all the values will result in EXACTLY the original amount.

If you find this library at all useful, or have improvements and suggestions, drop me a line at scot dot mcphee at gmail dot com

Please remember to shake your MoneyMaker.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • DZone
  • StumbleUpon
  • Technorati
  • TwitThis
  • LinkedIn