COM and currency conversion

Tim Peters tim.one at home.com
Sun May 6 22:24:21 EDT 2001


[John]
> 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)

Based on what Mark said later, here's a function that should do the trick:

TWO32 = 2L ** 32
def tuple_to_100x_pennies((hi, lo)):
    if lo < 0:
        lo += TWO32
    return (long(hi) << 32) + lo

Using your examples,

print map(tuple_to_100x_pennies,
          ((2, -539231788),
           (2, 1410065408),
           (4, -1474836480)))

prints

[12345670100L, 10000000000L, 20000000000L]

Divide by 100 to get pennies; by 10000 to get dollars.

[Mark Hammond]
> This is a little sad.  Python has no support at all for these.  The
> basic problem is that there is no Python type to convert it to.
> It you dig on MSDN, you will find this represents a fixed decimal
> point number.
> In the interests of being correct when Tim may be watching <wink> it
> is exactly:

Way to cover your ass, Mark!  I would have torn you to shreds <wink>.

> "A currency number stored as an 8-byte, two's complement
> integer, scaled by 10,000 to give a fixed-point number with 15
> digits to the left of the decimal point and 4 digits to the right.
> This representation provides a range of 922337203685477.5807 to
> -922337203685477.5808"
>
>   I have discussed this with Tim, and he agrees in general that
> conversion to float would be evil, and there is no better type.  He
> has suggested I rip out his "FixedPoint.py"
> (ftp://ftp.python.org/pub/python/contrib-09-Dec-1999/DataStructures/
>      FixedPoint.py)
> and use that.

I still do -- this particular format is just a special case of FixedPoint
with 4 digits after the decimal point, so should be easy to supply via
exposing a subclass of FixedPoint.

> However, there is also implication that Python will grow something
> like this as a core datatype, so I don't want to lock everyone
> into FixedPoint when something more natural may be just around
> the corner.

One person said they were going to look into this and was never heard from
again.  I'd like to implement IBM's proposed standard for decimal arithmetic
(http://www2.hursley.ibm.com/decimal/) but have no time for it (and never
thought I would <wink/sigh>).  Marc-Andre Lemburg started down some sort of
related path, but appeared to get distracted by wrapping the entire GNU
multiprecision library instead (which is way cool for its own sake, but
doesn't actually supply the decimal currency type he was looking for at the
start).

So don't hold your breath -- or at least don't hold mine <wink>.





More information about the Python-list mailing list