COM and currency conversion

Alex Martelli aleaxit at yahoo.com
Mon May 7 05:15:26 EDT 2001


"John" <notehead2 at hotmail.com> wrote in message
news:3wkJ6.6$Y95.2148 at newsread1.prod.itd.earthlink.net...
> I've run into another issue with using ADO/SQL Server with Python. It has
to
> do with how money fields come across in Python. Python converts the money
> field into a 2 value tuple. Anyone know the way to get a meaningful dollar
> about back out? Here are some examples of dollar amounts and their
> representation in Python:
>
> $1,234,567.01 == (2, -539231788)
> $1,000,000.00 == (2, 1410065408)
> $2,000,000.00 == (4, -1474836480)

It's not __too__ hard to get the meaningful dollar amounts back
out, based on the indications you have already received from Mr
Hammond and others.  For example:

>>> def dollars_as_float(a):
...    return float((long(a[1])&0xFFFFFFFFL) |
...        (long(a[0]) << 32)) / 1e4
...
>>> print dollars_as_float((2, -539231788))
1234567.01
>>> print dollars_as_float((2, 1410065408))
1000000.0
>>> print dollars_as_float((4, -1474836480))
2000000.0


Yeah, it IS ugly, but perhaps it will help you deal with COM
currency values until something better comes along.


Alex






More information about the Python-list mailing list