Alternative to Decimal type

Paul Hankin paul.hankin at
Mon Jun 9 10:54:31 CEST 2008

On Jun 9, 5:11 pm, Frank Millman <fr... at> wrote:
> I have a standard requirement for a 'decimal' type, to instantiate and
> manipulate numeric data that is stored in a database. I came up with a
> solution long before the introduction of the Decimal type, which has
> been working well for me. I know the 'scale' (number of decimal
> places) of the number in advance. When I read the number in from the
> database I scale it up to an integer. When I write it back I scale it
> down again. All arithmetic is done using integers, so I do not lose
> accuracy.
> There is one inconvenience with this approach. For example, if I have
> a product quantity with a scale of 4, and a price with a scale of 2,
> and I want to multiply them to get a value with a scale of 2, I have
> to remember to scale the result down by 4. This is a minor chore, and
> errors are quickly picked up by testing, but it does make the code a
> bit messy, so it would be nice to find a solution.
> I am now doing some refactoring, and decided to take a look at the
> Decimal type. My initial impressions are that it is quite awkward to
> use, that I do not need its advanced features, and that it does not
> help solve the one problem I have mentioned above.
> I therefore spent a bit of time experimenting with a Number type that
> suits my particular requirements. I have come up with something that
> seems to work, which I show below.
> I have two questions.
> 1. Are there any obvious problems in what I have done?
> 2. Am I reinventing the wheel unnecessarily? i.e. can I do the
> equivalent quite easily using the Decimal type?

Hi Frank,
I don't know why you think Decimal is complicated: it has some
advanced features, but for what you seem to be doing it should be easy
to replace your 'Number' with it. In fact, it makes things simpler
since you don't have to worry about 'scale'.

Your examples convert easily:

from decimal import Decimal
qty = Decimal('12.5')
price = Decimal('123.45')

print price * qty
print qty * price
print (qty * price).quantize(Decimal('0.01'))

Paul Hankin

More information about the Python-list mailing list