The 'n' format specifier

Peter Otten __peter__ at web.de
Thu Oct 21 06:02:36 EDT 2010


Boštjan Mejak wrote:

> The locale.setlocale(category=locale.LC_NUMERIC, locale="Slovenian") 
> works like a charm in my application. Now the 'n format specifier works as
> I want. But tell me whether the 'n' forma specifier can be forced to round
> the float to just one decimal place. I know that the 'f' format specifier
> does that by specifying ".1f", but 'f' is not locale-aware. I have set the
> 'n' format specifier in my application like ".3n", which is okay if the
> returned number is two integers and one decimal, but is not okay if the
> returned number is one integer and two decimals, because I want just one
> decimal, always. How can I make that by using the 'n' format specifier?

Looks like you have to resort to:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
'de_DE.UTF-8'
>>> locale.format("%.1f", 12345.6789, grouping=True)
'12.345,7'





More information about the Python-list mailing list