SPUG: Working with currency

Bill Campbell bill at celestial.com
Thu Apr 2 23:04:17 PDT 2009

On Thu, Apr 02, 2009, Michael R. Wolf wrote:
> On Apr 2, 2009, at 8:46 PM, Yitzchak Scott-Thoennes wrote:
>> The only pre-existing solution that's going to work consistently is:
>> Store and manipulate in pennies; only use dollars on user input and  
>> output.
>> No modules required. :)
> A module-less solution was my first choice.  It's elegant (for the  
> "small and simple" definition of elegant).
> After an hour of digging, I can't find indications (much less an answer) 
> on when it's necessary to 'use bigint'.  In other words, what is the 
> moral equivalent of MAXINT, or at least a close approximation  
> (disregarding whatever legal fine-print exists) for a back of the  
> envelope proof-of-concept design?

That depends on the underlying hardware and the integer length.
Normal longs will handle things most small-to-medium businesses
normally deal with (until Hyperinflation hits which is coming RSN
to a neighborhood near you).  Dollar amounts are stored
internally as cents.  Addition and subtraction is a no-brainer,
but multiplication and division is a bit messier as one needs to
normalize to cents with appropriate rounding rules.

It would be in interesting exercise in OO programming and
overriding operators to create a class that would handle fixed
point decimal numbers properly.  I did something like this
several years ago in perl, but generally find dealing with
bigints to be pretty ugly.

INTERNET:   bill at celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
Fax:            (206) 232-9186

When the customer has beaten upon you long enough, give him what he asks
for, instead of what he needs.  This is very strong medicine, and is
normally only required once.
    -- The Consultant's Curse:

More information about the spug-list mailing list