On Monday 21 February 2011 01:55:28 Walter Bright wrote: > Kevin Bealer wrote: > > 1. To solve the basic problem the original poster was asking -- if you > > are working with simple decimals and arithmetic you can get completely > > accurate representations this way. For some cases like simple financial > > work this might work really well. e.g. where float would not be because > > of the slow leak of information with each operation. (I assume real > > professional financial work is already done using a (better) > > representation.) > > A reasonable way to do financial work is to use longs to represent pennies. > After all, you don't have fractional cents in your accounts. > > Using floating point to represent money is a disaster in the making.
Actually, depending on what you're doing, I'm not sure that you can legally represent money with floating point values. As I understand it, there are definite restrictions on banking software and the like with regards to that sort of thing (though I don't know exactly what they are). Regardless, if you want to deal with money correctly, you pretty much have to treat it as cents and use integer values. Anything else will cause too many errors. That's definitely the way I've seen it done at any company that I've work at which have had to deal with money in their software. - Jonathan M Davis