Version française
Home     About     Download     Resources     Contact us    
Browse thread
Data type representing 'Money'
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Data type representing 'Money'
On Sat, May 16, 2009 at 09:23:04PM -0500, Edgar Friendly wrote:
> Conglun Yao wrote:
> > Hi all,
> > 
> > Is there any data type could be used to represent 'Money'  in OCaml,
> > similar to BigDecimal.
> > I see a lot of programs using float directly. Maybe Num would be better,
> > but less document could be found, for example, how to represent 20.27
> > 
> > Or there already exist a better solution. Thanks for any help.
> > 
> > Conglun
> > 
> > 
> I'd do fixed point math using Num to count cents (or mills if you really
> need) but displaying dollars and cents.  For example: $20.27 would
> internally be represented by the number 2027, and use int arithmetic for
> exact math.
> 
> If you used 63-bit ints, you'd probably get away with not even using
> Num, as max_int = 4611686018427387903 would represent the dollar amount
> 46_116_860_184_273_879.03, just short of 50 quadrillion dollars.

But be careful if you want to use the software on 32 bit machines.
You won't even be able to represent a single mortgage loss, nevermind
the size of your bailout!

Rich.

-- 
Richard Jones
Red Hat