Python, mysql, roundoff problem?

Sam Penrose sam at ddmweb.com
Mon Jul 30 18:35:18 EDT 2001


"William Dandreta" <wjdandreta at worldnet.att.net> wrote:
> I have done that in the past. The reason I asked about 64 bit integers
> is because I need more than 2 decimal places. Some items are
> inexpensive and sold by the 1000, i.e., $12.57 per 1000 => .01257 each.

> Greg Jorgensen wrote in message
>>"William Dandreta" <wjdandreta at worldnet.att.net> wrote:
>>
>>> mysql stores numbers as strings. I discovered that when converted to floats
>>> in Python they are not exactly the same which can cause roundoff errors. The
>>> floats represent money and I need to be sure there is no roundoff errors.
>>>
>>> What is normally done to deal with this situation?
>>
>> Work with pennies instead of dollars. Store 12949 instead of 129.49.
>> Do integer arithmetic with pennies rather than floating-point
>> arithmetic with >dollars. Divide by 100 when you need to display the
>> value.

There appear to be two kinds of solutions to the problem of decimal
arithmethic: real ones and hacks. Real ones seem to require a lot of
hard work (Aahz Maruch is working on one) or that you be Tim Peters
(search for FixedPoint.py). Actually, the fifth line of FixedPoint says
"use at your own risk", so maybe that one isn't real. Aahz's isn't
released yet, AFAIK, so it probably doesn't qualify as real yet either. 

In the hack category, I wrote a module that I use on numbers passed
into and out of MySQ that works for me <wink>, in that you can do
sensible arithmetic with it. I posted it here and received almost no
feedback, which is about what it deserved. If there is any interest, I
would be happy to post an updated version.




More information about the Python-list mailing list