SPUG: Working with currency

Colin Meyer cmeyer at helvella.org
Thu Apr 2 16:23:10 PDT 2009

On Thu, Apr 02, 2009 at 06:10:53PM -0400, Michael R. Wolf wrote:
> I've got some students in class this week whose process was halted  
> because they were off by $0.02 in their $3M totals.
> A quick CPAN search unearthed Math::Currency.  If anyone has  
> experience with it, I'd appreciate a quick "thumbs up" or "thumbs  
> down" quick poll.  I'd also appreciate any quick pointers to useful  
> modules or practices (or anti-recommendations or anti-patterns).

Were your students trying to convert between different currencies?

That's what Math::Currency seems geared towards.

If they are trying to do money calculations, and are not worried
about converting between foreign currencies, then the quick answer
is to do all calculations in pennies, as integers, and only convert
to dollars when displaying. This avoids propagating floating point
rounding errors. The long answer is quite ... well, longer. There's 
lots on the web about calculations with money.

> I don't need a *description of the problem*.  I understand the  
> limitations of software and hardware.  What I'm looking for is some  
> pointers to *pre-existing solutions*.

Er, it might help to have a description of the problem, in order to
help you find answers. Solution to what? $0.02 off? Just add (or
substract, as the case may be) that two cents. :P


More information about the spug-list mailing list