Converting from integer number of cents to ASCII and vice versa is pretty
easy to implement from scratch.

If your repeatedly use % 10 and / 10 to extract the least significant digit
and shift down the remaining digits, you can convert from integer to ASCII
one digit at a time. Of course, you'll be getting the digits in reverse
order, so you'll probably want to allocate a fixed size string for the
output, and fill in the digits from right to left. Shifting over the
rightmost 2 digits and inserting a decimal separator is easy enough.

For converting back from ASCII to integer, you could start by removing the
decimal point from a copy of the string, then go through the digits from
right to left. For each digit, convert it from ASCII to a number from 0 to
9, multiply the result by 10 and add in the new digit. The result starts out
as 0. If you need to handle negative numbers, you'll have a bit more work to
do.

Adding and subtracting can be done directly on the integers. Multiplying by
an integer multiple (like 2) can be done by directly multiplying integers.
If you want to multiply by a decimal amount (like 0.51), you'll need to
divide by a power of ten as well.

Using a floating point type to store dollar amounts is risky because if the
point floats too far, you can end up losing precision necessary to represent
down to the penny. If this happens, you get no errors, and the result comes
out about right, but not quite right. That's not the kind of error you want
in financial calculations!
-- 
Peter Epstein

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to