views:

355

answers:

4

What class should I use for representation of money to avoid most rounding errors?

Should I use Decimal, or a simple built-in number?

Is there any existing Money class with support for currency conversion that I could use?

Any pitfalls that I should avoid?

+3  A: 

You might be interested in QuantLib for working with finance.

It has built in classes for handling currency types and claims 4 years of active development.

Jweede
This project looks interesting, but may be a bit too much, I will most likely use a pure python project for simplicity.
voyager
+3  A: 

You could have a look at this library: python-money. Since I've no experience with it I cannot comment on its usefullness.

A 'trick' you could employ to handle currency as integers:

  • Multiply by 100 / Divide by 100 (e.g. $100,25 -> 10025) to have a representation in 'cents'
ChristopheD
Many accounting systems track things far more precisely than to the cent.
Paul McMillan
True, but all depends on the needs I guess... The quantlib library referenced in this thread seems a good candidate for 'serious' financial work.
ChristopheD
+3  A: 

I assume that you talking about Python. http://code.google.com/p/python-money/ "Primitives for working with money and currencies in Python" - the title is self explanatory :)

juckobee
That is what tags are for :)
voyager
Looking at http://code.google.com/p/python-money/source/browse/trunk/money/Money.py I can see that they do use `Decimal` for internal representation :)
voyager
+1  A: 

Just use decimal.

S.Lott