Alternative to Decimal type
frank at chagford.com
Wed Jun 11 16:14:55 CEST 2008
On Jun 11, 4:39 pm, Ethan Furman <et... at stoneleaf.us> wrote:
> Frank Millman wrote:
> > Thanks to all for the various replies. They have all helped me to
> > refine my ideas on the subject. These are my latest thoughts.
> Out of curiosity, what is the purpose of these numbers? Do they
> represent money, measurements, or something else?
I am writing a business/accounting application. The numbers represent
mostly, but not exclusively, money.
Multiply a selling price (scale 2) by a product quantity (scale 4) to
get an invoice value (scale 2), rounded half-up.
Multiply an invoice value (scale 2) by a tax rate (scale 2) to get a
tax value (scale 2), rounded down.
Divide a currency value (scale 2) by an exchange rate (scale 6) to get
a value in a different currency (scale 2).
Divide a product quantity (scale 4) by a pack size (scale 2) to get an
equivalent quantity in a different pack size.
In my experience, the most important thing is to be consistent when
rounding. If you leave the rounding until the presentation stage, you
can end up with an invoice value plus a tax amount differing from the
invoice total by +/- 0.01. Therefore I always round a result to the
required scale before 'freezing' it, whether in a database or just in
an object instance.
More information about the Python-list