[Python-Dev] Formatting mini-language suggestion

Raymond Hettinger python at rcn.com
Thu Mar 12 03:50:13 CET 2009


[James Y Knight]
> You might be interested to know that in India, the commas don't come  
> every 3 digits. In india, they come every two digits, after the first  
> three. Thus one billion = 1,00,00,00,000. How are you gonna represent  
> *that* in a formatting mini-language? :)

It is not the goal to replace locale or to accomodate every
possible convention.  The goal is to make a common task easier
for many users.  The current, default use of the period as a decimal 
point has not proven to be problem eventhough that convention is
not universal.   For a thousands separator, a comma is a decent choice 
that makes it easy follow-on with s.replace(',', '_') or somesuch.

This simple utility could help a lot of programmers make their output
look more professional and readable.  I hope the idea doesn't get
sunk by a desire to over-parameterize and cover every possible use case.

My pocket calculators all support thousands separators but in Python,
we have to do a funky dance for even this most basic bit of formatting.

I'd like to think that in 2009 we could show a little progress beyond
C's printf() or Fortran's write() formats.


Raymond



>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
>>> conv = locale.localeconv()          # get a mapping of conventions
>>> x = 1234567.8
>>> locale.format("%d", x, grouping=True)
'1,234,567'



More information about the Python-Dev mailing list