[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


Hi,

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')
'0,000,000,000,001,234'
>>> format(float(1234), '0=20,g')
'0,000,000,000,001,234'
>>> 
>>> format(Decimal(1234), '020,g')
'0,000,000,000,001,234'
>>> format(Decimal(1234), '0=20,g')
'0000000000000001,234'

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


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