[Python-ideas] Rough draft: Proposed format specifier for a thousands separator (discussion moved from python-dev)

Eric Smith eric at trueblade.com
Fri Mar 13 00:31:39 CET 2009


Eric Smith wrote:
> I've always thought that we should have a utility function which formats 
> a number based on the same settings that are in the locale, but not 
> actually use the locale. Something like:
> 
> format_number(123456787654321.123, decimal_point=',', thousands_sep=' ',
>               grouping=[4, 3, 2])
>  >>> '12 34 56 78 765 4321,123'

To be maximally useful (for example, so it could be used in Decimal to 
implement locale formatting), maybe it should work on strings:

 >>> format_number(whole_part='123456787654321',
               fractional_part='123',
               decimal_point=',',
               thousands_sep=' ',
               grouping=[4, 3, 2])
 >>> '12 34 56 78 765 4321,123'

 >>> format_number(whole_part='123456787654321',
               decimal_point=',',
               thousands_sep='.',
               grouping=[4, 3, 2])
 >>> '12.34.56.78.765.4321'

I think such a method, along with locale.localeconv(), would be the 
workhorse for much of formatting we've been talking about. It could be 
flushed out with the sign and other remaining fields from localeconv(). 
The key point is that it takes everything as parameters and doesn't use 
any global state. In particular, it by itself would not reference the 
locale.

I'll probably add such a routine anyway, even if it doesn't get 
documented as a public API.

Eric.




More information about the Python-ideas mailing list