From: Brian Hurt <bhurt@spnz.org>
To: Joshua Smith <kognate@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] What library to use for arbitrary precision decimals
Date: Sun, 19 Feb 2006 09:44:39 -0600 (CST) [thread overview]
Message-ID: <Pine.LNX.4.63.0602190934300.17832@localhost.localdomain> (raw)
In-Reply-To: <d68fa980602190543g46a86620xfdd584ca2110768d@mail.gmail.com>
On Sun, 19 Feb 2006, Joshua Smith wrote:
> There are a couple of ways to handle money transactions without
> rounding errors.
>
> You could use the Nums library, which provides arbitrary precision
> calculations and numbers. But even with arbitrary precision numbers,
> you still can have the situation where you get 405.0345 which if this
> were USD, you would still have to round if you wanted to pay someone
> this amount. You will still have the arbitrary precision this way,
> however.
>
> The best way to handle money (in my experience) is to use integers.
> Then you can define conversion functions but only have to convert it
> to decimal for display purposes. That way, even if you do a million
> transactions you won't lose any information. You can also handle
> non-decimal based currencies/instruments/transactions that way[1]
I agree with this recommendation. The basic idea is that you use fixed
point. Say you want to be accurate to one thousandth of a cent (0.001
cents). You simply do all calculations in terms of millicents. So the
integer 1 represents one millicent. One penny is represented as the
integer 1,000- one thousand millicents. The amount $2,345.67 is
represented by the integer 234,567,000.
If you use the Int64 module as the basis for your computations, you can
hold values up to $184,467,440,737,095.51 in size. This is larger than
the world's GDP, so it should be large enough. 32 bits isn't enough- that
only allows you to hold values up to $42,949.67.
Brian
next prev parent reply other threads:[~2006-02-19 15:44 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-02-19 13:43 Joshua Smith
2006-02-19 15:44 ` Brian Hurt [this message]
2006-02-19 16:38 ` [Caml-list] " Richard Jones
2006-02-19 16:53 ` Brian Hurt
2006-02-19 17:00 ` Richard Jones
2006-02-19 17:13 ` Richard Jones
2006-02-19 17:21 ` Jeremy Shute
2006-02-19 18:31 ` Brian Hurt
-- strict thread matches above, loose matches on Subject: below --
2006-02-19 10:18 Richard Jones
2006-02-19 14:03 ` [Caml-list] " Christophe TROESTLER
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.63.0602190934300.17832@localhost.localdomain \
--to=bhurt@spnz.org \
--cc=caml-list@yquem.inria.fr \
--cc=kognate@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox