[Python-Dev] Sort out formatting differences in decimal and float

Stefan Krah stefan-usenet at bytereef.org
Sat Dec 5 12:18:47 CET 2009


I'm in the process of implementing formatting in my C-decimal module.
Since testing is quite time consuming, I would appreciate a binding
decision on how the following should be formatted:

>>> from decimal import *
>>> format(float(1234), '020,g')
>>> format(float(1234), '0=20,g')
>>> format(Decimal(1234), '020,g')
>>> format(Decimal(1234), '0=20,g')

>>> format(Decimal('nan'), '020,g')
'                 NaN'
>>> format(Decimal('nan'), '0=20,g')

You can see that float literally follows PEP-3101:

"If the width field is preceded by a zero ('0') character, this enables
 zero-padding.  This is equivalent to an alignment type of '=' and a
 fill character of '0'."

The advantage of decimal is that the user has the option to suppress
commas. The behaviour of float is slightly easier to implement in C.

So the options are:

   a) 020 is always equivalent to 0=20

   b) 020 is not always equivalent to 0=20

Stefan Krah

More information about the Python-Dev mailing list