[Haskell-cafe] What is a safe Haskell data type to store and manipulate Money values?

David Turner dct25-561bs at mythic-beasts.com
Thu Apr 6 06:32:39 UTC 2017


On 5 Apr 2017 04:15, "Richard A. O'Keefe" <ok at cs.otago.ac.nz> wrote:


> On 4/04/2017, at 11:21 PM, Saurabh Nanda <saurabhnanda at gmail.com> wrote:

> * 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.


Careful here. I've worked on systems where one of the fundamental
requirements is that any sums of columns of numbers representing amounts of
money _must_ add up precisely. In a situation where you apply different tax
rates to different items you could either calculate the tax on each one
(and round it to £0.01) or group the items together into subtotals that all
had the same tax rate, then calculate the tax on the subtotal (and round it
to £0.01). What you couldn't do was keep track of the precise amount of tax
on each item and round it at the very end for reporting purposes only,
because you had to show the tax breakdown (using numbers rounded to £0.01)
and if your working calculation was more precise then the numbers in the
report wouldn't always quite add up, which would upset the auditors.

This is why it's frequently recommended to use an integral type
representing multiples of your smallest reporting unit for representing
money.

The rounding rule is *probably* the one
you learned in school, but you really should check with a friendly
accountant.


The actual rounding rule in question seemed relatively unimportant compared
with the requirement that numbers in reports must add up precisely,
although I also agree that you should check with an accountant. They may
not even mention the columns-of-numbers-must-add-up thing because that's so
fundamental it almost goes without saying.

Cheers,

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170406/a841f7d2/attachment.html>


More information about the Haskell-Cafe mailing list