v2 of my app HandyShopper uses Long to represent cents.

however, i discovered very quickly that fixed-point math is limited.  think
about longhand multiplication (since this is what you're emulating).

let's take 400.00 and 1200.00 for instance.  we convert them to 40000 and
120000 as Longs, and now we multiply them.  whups, overflow.  the result is
4,800,000,000 which is larger than 32 bits (much less 31 to allow negative
numbers).  so the problem is that although you can divide by 100 afterwards
(to get us back to only 2 "decimal" places) and the final result would
actually fit, it has already overflowed before you have an opportunity to
shift the decimal place correctly.

my solution?  i store the numbers as Longs, but when doing arithmetic
calculations, i convert them to FlpDouble and use NewFloatMgr to manipulate
them.  once i've finished my calculations, i convert the final result back
to a Long.

part of the reason i ran into this problem is because my app has a
calculator, and it's not reasonable for a calculator to overflow so easily.
the other reason is that my app allows Quantity and Price, and multiplies
them together.  the fixed-point problem demonstrated above becomes even more
pronounced since i have 2 fixed decimal places for Price, but 3 fixed
decimal places for Quantity.


----- Original Message -----
From: dpw Designs <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, September 12, 1999 9:11 AM
Subject: Re: What's the best Currency Format?


> Luiz Paulo Gama Barreto wrote:
> >
> > Hi all,
> > I'm wondering about the best format to work with currencies.
> > It would be a float format?  Or maybe work with the values in cents, in
a ULong format,
>
> Work with the values in cents (fixed-point math with respect to
> dollars).  The concept here is to use integers, with implied decimal
> points.  For example, suppose you wanted to find
>
>      4.00 x 0.60
>
>      Which is equivalent to:
>
>        400      60       24000
>        ---  x  ---  =  ---------  =  2.4
>        100     100    (100 x 100)
>
>      HINT: Imagine decimal points painted in, two digits from the right.
>
>
> So, all you need to do it accept input from a field.  Multiply the
> number by your ScaleFactor (100 in the above example) to turn it into a
> fixed-point integer.  Perform your calculation (adjusting it as shown
> below).  Finally, dividing it by your Scale Factor.
>
>         Operation       Adjustment
>
>         Add/Subtract    No Adjustment required
>         Multiply        Divide by ScaleFactor
>         Divide          Multiply by ScaleFactor
>
>
> --
>  ___________________________________________________________________
>   dpw Designs                    http://www.pe.net/firm/dpw-designs
>                                  mailto:[EMAIL PROTECTED]
>

Reply via email to