Python, databases, floating point, and money

Jarek Zgoda jzgoda at usun.gazeta.pl
Fri Dec 6 17:13:04 EST 2002


Dnia 6 Dec 2002 21:28:57 GMT
<klaatu at evertek.net> pisze:

> What is the accepted method of dealing with monetary calculations and
> databases, specifically in Python, but also in general?  I know that
> there are floating point types, but it seems to me that their
> imprecision would cause significant problems when dealing with
> monetary quantities, once those numbers get very large at all.
> 
> One scheme I've thought of is to use two fields - an integer (probably
> 64bit, internally represented as a Python long) representing the
> quantity, and then another integer field that has the number of places
> to the right of the zero (i.e., the power of 10 to divide by to get
> the true number).  But it seems like this would be horribly
> non-standard.

Usually in databases there's some datatype (named DECIMAL or NUMERIC)
which is a "binary coded decimal" - that is fixed point decimal. It
depends on implementation, but usually internally it is coded as int64
(as in Interbase) or some compound type (two int32 forming a structure,
as in MSSQL2k). Usually client libraries perform some translation to
double precision, if host language doesn'thandle native types.

-- 
~$ rm -f /bin/Laden
I po kłopocie!

-- 
Serwis Usenet w portalu Gazeta.pl -> http://www.gazeta.pl/usenet/



More information about the Python-list mailing list