help needed on decimal formatting issue

Mark Dickinson mdickinson at enthought.com
Mon Sep 19 18:12:01 CEST 2011


On Sep 19, 1:42 pm, Robin Becker <ro... at reportlab.com> wrote:
> I'm not really very used to the decimal module so I'm asking here if any one can
> help me with a problem in a well known third party web framework
>
> The code in question is
>
> def format_number(value, max_digits, decimal_places):
>      """
>      Formats a number into a string with the requisite number of digits and
>      decimal places.
>      """
>      if isinstance(value, decimal.Decimal):
>          context = decimal.getcontext().copy()
>          context.prec = max_digits
>          return u'%s' % str(
>           value.quantize(decimal.Decimal(".1") ** decimal_places,
>                         context=context))
>      else:
>          return u"%.*f" % (decimal_places, value)

What's the meaning of the 'max_digits' argument here?  Are you
guaranteed that the incoming value is smaller than 10**max_digits in
absolute value?

If so, then a precision of max_digits + decimal_places + 1 should be
enough.  The +1 is there to cover the corner case where a value close
to 10**max_digits is rounded up to 10**max_digits by the quantize
operation.

BTW, that's a fairly horrible way of creating the first argument to
the quantize method, too.  It would be more efficient to do something
like:

>>> decimal_places = 2
>>> decimal.Decimal('0.{}1'.format('0'*(decimal_places-1)))
Decimal('0.01')

(perhaps with suitable special-casing for the case where
decimal_places <= 0;  not sure whether this applies in your context).

--
Mark



More information about the Python-list mailing list