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

Manuel Gómez targen at gmail.com
Tue Apr 4 05:05:36 UTC 2017


On Mon, Apr 3, 2017 at 9:50 PM Sandeep Cr <sandeepcr2 at gmail.com> wrote:
> What is a good data type for storing and manipulating monetary values in a typical web application with payment processing? What are the pros and cons of various options like Data.Scientific, Rational and Decimal?

It depends on what sort of computation you'll be doing upon those
monetary values and on the expectations of your users.  It's typical
that payment processing applications sacrifice precision and instead
prefer to deal in exact quantities, in which case you probably want
some fixed-precision type with cents, and you'll want to be quite
careful about your arithmetic.  I insist that this is not necessarily
what you want depending on the expectations of your users and I would
recommend reviewing the usual go-to piece of literature on the hazards
of numerical computing:
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

I would say you ought to pick your semantics first, and then find the types
that match, which is indeed secondary.  Avoid any universal
nuance-free advice like "never use floating point" or "use integers
for cents" as conventional wisdom on this topic is often wrong for
many applications.  Check whether regulatory compliance affects your
choice of semantics.


More information about the Haskell-Cafe mailing list