SPUG: Working with currency
haircut at gmail.com
Thu Apr 2 20:01:47 PDT 2009
Sorry in advance, this drifts offtopic a bit.
>> This may be part of your longer answer: wouldn't integer calculations in
>> fractional cents (to two or three decimal places) be closer to a
>> real-world solution?
> No, you want to do calculations in units of pennies and round to the nearest
> penny (using banker's rounding) whenever you calculate a value that will be
> displayed to the customer (unless you are displaying values other than to
> the nearest penny, which is rare but not unheard of).
> And you make these calculation in floating point, which is what Perl does
> even if you use int() (but don't "use integer;").
Be sure to get *very* specific requirements on rounding, especially
when dealing with multiple currencies. See:
My current software project (*cough* Mifos *cough*) is forced to allow
banks to perform rounding however they see fit. Different banks might
use different methods, and (as mentioned in the above link to
xencraft.com) different countries may proscribe different rounding
rules. I think rounding is usually done when credits and debits are
posted (both simultaneously in double-entry accounting) but until
then, it may be beneficial to store fractional monetary amounts.
More information about the spug-list