[Haskell-cafe] What is a safe Haskell data type to store and manipulate Money values?
Richard A. O'Keefe
ok at cs.otago.ac.nz
Wed Apr 5 03:14:37 UTC 2017
> On 4/04/2017, at 11:21 PM, Saurabh Nanda <saurabhnanda at gmail.com> wrote:
> Is there a document which explains common use-cases for monetary values and best-practices for dealing with them? Any guidelines for which **Haskell** data-types to use for what kind of monetary calculations?
I think you may need to talk to an accountant to find out what rounding
rules are required for your application. As long as you are just adding
and subtracting amounts of money, integer numbers of cents work fine.
As soon as you start multiplying by percentages (to compute discounts,
penalties, or taxes -- hello VAT, GST, &c -- it gets trickier.
> Here is what we are doing in our app:
> * Powering web-based e-commerce checkout flows
> * Allowing store owners to input tax rates
Ah. There you go. A 15% GST on $1.37 is 20.55 cents.
We used to have a 12.5% GST, and 12.5% of $1.36 is 17.125 cents.
> * Allowing end-customers to see product prices in different currencies (so, currency conversion)
> * Various reports to see total sales, total receivables, and total payables (basically a **very** small subset of small-business accounting)
There are Haskell data types that will let you compute sums and
differences of money times percentages exactly. For final reporting,
you will need to round. The rounding rule is *probably* the one
you learned in school, but you really should check with a friendly
> * Exporting data to full-fledged accounting systems, like Quickbooks and Xero.
> Based on this use-case, which Haskell data-type would you suggest?
Data.Decimal doesn't do a lot, but it probably does almost everything you
need. What it *doesn't* offer is operations with controlled rounding or
much in the way of rounding methods (if I recall correctly IEEE decimal
floats offer seven different rounding modes and the extra three, compared
with binary floats, were added to support commercial calculations).
It's not going to be terribly hard to program whatever rounding you need.
More information about the Haskell-Cafe