General Ledger/Accounting Libraries

Frank Millman frank at chagford.com
Thu May 13 11:27:48 EDT 2004


aahz at pythoncraft.com (Aahz) wrote:
> In article <mailman.422.1084270942.25742.python-list at python.org>,
> Andreas Pauley  <python-list at qbcon.com> wrote:
> >
> >Our company is currently evaluating Python as a language for writing
> >financial/accounting type software (among others).
> >
> >What libraries or packages are available in this domain for use?
> 
> The most important library is the new Decimal package, which hasn't been
> released for production yet.  You could start by using FixedPoint instead
> until Python 2.4.  Unfortunately, neither includes support for some of
> the more complicated calculations you want to do; the Python community
> will welcome your assistance on this (there are other people who also
> want to use Python for financial calculations, and Decimal is the first
> step).
> 

I have found that doing accurate decimal arithmetic is fiddly, but not
impossible. This is how I do it in my system.

In my database, I declare the various numeric columns with the maximum
scale that I think will be necessary. Monetary columns have a scale of
2, quantity columns have a scale of 6, etc. When I read the data in, I
scale it up so that I store it internally as an integer. For data
input/display purposes, I have a user-defined scale for each column,
which can either be absolute (eg all quantities are shown to 2
decimals) or parameter (eg get the scale for each product from the
product table, so they can vary). When I write back to the database, I
scale down from the internal integer to the scale defined in the
database.

It does lead to some awkward code. For example, to calculate a value
from a quantity and a price, I have to say

    value = int(round(price * value / 1000000.0))

This requires you to know what scale is used for each column, so
mistakes are easy to make. As you say, the Decimal package, when it is
released, will make life much easier.

Frank Millman



More information about the Python-list mailing list