From: Harry Ohlsen Date: 2002-12-11T05:50:07+09:00 Subject: Re: Proper Type for Dollar Amount On Wed, 11 Dec 2002 05:46, Mark Probert wrote: > At 03:14 AM 12/11/2002 +0900, Bill T. wrote: > >Jim Weirich wrote: > > > Yes, they were using floating point to represent dollar > > > amounts. Never a good idea. > > > >Well, you can search comp.lang.c about a thread on what type to use > >(int/long/double/etc.) to represent dollar amounts (including the > >cents). I don't think there is a clear answer that representing dollar > >amounts with floating point is a bad idea. It depends on what kinds of > >operations you are going to do, which in this case is a division. > > My own experience (based on developing real-time trading systems for > some merchant banks), is that the best solution is to use scaled > integers, to 100th of a cent. So, $154.34 --> 1543400. You then > get very fast operations (they are all integer operations) and, in > general, well with +- 4 billion. Ruby, with BigInt makes it trivial. Normally, I think financial people like to use fixed-point stuff, so that they don't get any kind of strange rounding errors, like adding up a bunch of numbers that should be precisely $1.00 and getting $99.997 or something like that. Maybe there's room for a Money class that uses the encoding suggested above (ie $154.34 is represented as 1543400), but has an appropriate to_s (not sure whether you'd need anything else). For that matter, I guess a more generic FixedPoint class could be set up and Money could be a subclass of that. I realise there are probably issues like ... what does you do if you have X dollars and want to do calculations based on it and some non FixedPoint value, like an interest rate of 6.25%. I guess you'd need appropriate conversions between floating point and FixedPoint. In any case, I'm sure these are things that people who do financial calculations have thought about long and hard and have appropriate solutions for. Harry O.