[Haskell-cafe] money type ?

Jeremy Shaw jeremy.shaw at linspireinc.com
Thu Jul 12 13:53:14 EDT 2007

At Thu, 12 Jul 2007 00:42:46 -0700,
Simon Michael wrote:
> Good day all,
> my budding ledger program could not balance transactions exactly because of 
> rounding error with Double. I *think* I got it working better with Rational 
> (it was late). Another suggestion from #haskell was to multiply all money 
> by 100. I'm tracking multiple currencies/commodities with varying precision 
> so this gets a bit more complicated.
> Is there a type or library out there that's good for representing money and 
> other quantities while avoiding rounding errors  ?

Almost. I have a mostly complete Decimal library for Haskell available at:


It aims to meet the same goals the Python Money PEP:


Most of the implementation details are described in this standard:


Currently there are two very significant bugs:

 1. the rounding algorithm is completely wrong

 2. In the division routine, I explicitly limit the number of digits
    after the decimal point to 9 places.

Using the Decimal library you can then implement a Money type similar
to what is show in:


essentially, you declare a phantom data type like:

> data Money currency = Money Decimal

which you can then parameterize by different types of currency

> data Dollar = Dollar
> data Yen = Yen

> fiveDollars :: Money Dollar
> fiveDollars = 5

> fiveYen :: Money Yen
> fiveYen = 5

because the types are different, you won't be able to accidently
charge someone 5 yen instead of 5 dollars :)

However, you can still write currency agnostic functions:

> doubleMyIncome :: Money c -> Money c
> doubleMyIncome income = income * 2.0

So, the decimal library is almost, but not quite, usable. Fixing the
two (known) problems is not that hard, I just have not had the
time. Also, the library really needs a good test suite before it can
be considered suitable for an accounting program ;) Fortunately, the
python library already has a good test suite that can copied. I
believe the spec at IBM also includes a bunch of examples that could
be interegrated into a test suite. The library includes the beginnings
of a test suite, but it is not very complete.

I am definitely willing to give guidance and assistance if you, or
someone else, wants to help finish up the library.


More information about the Haskell-Cafe mailing list