Rough draft: Proposed format specifier for a thousands separator

Raymond Hettinger python at rcn.com
Fri Mar 13 11:41:39 CET 2009


[Lie Ryan]
> A hyphotetical code using conv function and the microlanguage could look
> like this:
>
>  >>> num = 213210.3242
>  >>> fmt = create_format(sep='-', decsep='@')
>  >>> print fmt
> 50|\/|3_v3ry_R34D4|3L3_C0D3
>  >>> '{0!{1}}'.format(num, fmt)
> '213-210 at 3242'

LOL, it's like APL all over again ;-)

FWIW, the latest version of the proposal is dirt simple:

>>> format(1234567, 'd')       # what we have now
'1234567'
>>> format(1234567, ',d')      # proposed new option
'1,234,567'
>>> format(1234.5, '.2f')      # what we have now
'1234.50'
>>> format(1234.5, ',.2f')     # proposed new option
'1,234.50'


The proposal is roughly:
  If you want commas in the output,
  put a comma in the format string.
It's not rocket science.

What is rocket science is what you have to do now
to achieve the same effect.  If someone finds the
above to be baffling, how the heck are they going
to do the same thing using the locale module?


Raymond



More information about the Python-list mailing list