COM and currency conversion

Mark Hammond MarkH at ActiveState.com
Sun May 6 20:58:50 EDT 2001


John wrote:

 > 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)

      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:
"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.  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.

Mark.




More information about the Python-list mailing list