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

Mark Dickinson dickinsm at gmail.com
Sat Dec 5 13:23:26 CET 2009


On Sat, Dec 5, 2009 at 11:18 AM, Stefan Krah <stefan-usenet at bytereef.org> wrote:
> 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'

I like the Decimal behaviour better, but then I might be a little biased.  :-)

I find it odd that, for float formatting, the choice of fill character
affects whether commas are inserted:

Python 3.2a0 (py3k:76671, Dec  4 2009, 18:55:54)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> format(float(1234), '0=20,g')
'0,000,000,000,001,234'
>>> format(float(1234), '1=20,g')
'1111111111111111,234'
>>> format(float(1234), 'X=20,g')
'XXXXXXXXXXXXXXX1,234'

Mark


More information about the Python-Dev mailing list