This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Format specifier 'n' not working
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Retro, eric.araujo, eric.smith, georg.brandl, rhettinger
Priority: normal Keywords:

Created on 2010-10-18 10:26 by Retro, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (8)
msg119003 - (view) Author: Boštjan Mejak (Retro) Date: 2010-10-18 10:26
Have your default locale set to 'Slovenian' which uses a comma for the decimal symbol, like 2.76 is written as 2,76. Observe that the format specifier 'n' does not work for the default 'Slovenian' locale setting.

Then try running this in the Python interpreter:
'{number:.3n}'.format(number=2.76)

You get this:
>>> '{number:.3n}'.format(number=2.76)
'2.76'

Excepted the is the return value '2,76'. Please fix this bug.
msg119008 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-10-18 11:44
How are you setting the Slovenian locale?

Could please let me know what this prints:

import locale
locale.localeconv()

Also, could you let me know what:
locale.format('%.3f', 2.45)

prints? Both of these would be when the Slovenian locale is selected.

Thanks.
msg119010 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-10-18 12:00
Oops, I meant "locale.format('%.3f', 2.76)", although of course the number shouldn't matter.
msg119042 - (view) Author: Boštjan Mejak (Retro) Date: 2010-10-18 17:23
{'mon_decimal_point': '', 'int_frac_digits': 127, 'p_sep_by_space': 127,
'frac_digits': 127, 'thousands_sep': '', 'n_sign_posn': 127,
'decimal_point': '.', 'int_curr_symbol': '', 'n_cs_precedes': 127,
'p_sign_posn': 127, 'mon_thousands_sep': '', 'negative_sign': '',
'currency_symbol': '', 'n_sep_by_space': 127, 'mon_grouping': [],
'p_cs_precedes': 127, 'positive_sign': '', 'grouping': []}

'2.760'

And the output of locale.format() in this case should be
'2,760'

On Mon, Oct 18, 2010 at 2:00 PM, Eric Smith <report@bugs.python.org> wrote:

>
> Eric Smith <eric@trueblade.com> added the comment:
>
> Oops, I meant "locale.format('%.3f', 2.76)", although of course the number
> shouldn't matter.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue10135>
> _______________________________________
>
msg119045 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-10-18 17:32
It looks like the float.__format__ code is correctly using the value of decimal_point = '.'. It also agrees with locale.format().

How are you setting the locale? The problem appears to be either how you are setting the locale or the contents of the locale, not in the formatting code.
msg119047 - (view) Author: Boštjan Mejak (Retro) Date: 2010-10-18 17:56
So do I need additional things to set in my code if I use the 'n' format
specifier in order for it to be locale-aware? So using just the 'n' format
specifier is not enough? Please explain in depth.

On Mon, Oct 18, 2010 at 7:32 PM, Eric Smith <report@bugs.python.org> wrote:

>
> Eric Smith <eric@trueblade.com> added the comment:
>
> It looks like the float.__format__ code is correctly using the value of
> decimal_point = '.'. It also agrees with locale.format().
>
> How are you setting the locale? The problem appears to be either how you
> are setting the locale or the contents of the locale, not in the formatting
> code.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue10135>
> _______________________________________
>
msg119053 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-10-18 18:14
You need to call setlocale() in your program.  For more in-depth explanations, please refer to the docs at <http://docs.python.org/library/locale>.
msg119057 - (view) Author: Boštjan Mejak (Retro) Date: 2010-10-18 18:18
I thought having "Slovenian" locale set in Windows OS is the way the 'n'
format specifier works. So I must set the locale in the app. Now we're
cookin'! ;) Thanks Georg.

On Mon, Oct 18, 2010 at 8:14 PM, Georg Brandl <report@bugs.python.org>wrote:

>
> Georg Brandl <georg@python.org> added the comment:
>
> You need to call setlocale() in your program.  For more in-depth
> explanations, please refer to the docs at <
> http://docs.python.org/library/locale>.
>
> ----------
> nosy: +georg.brandl
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue10135>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:57:07adminsetgithub: 54344
2010-10-19 10:29:19eric.smithsetstage: resolved
2010-10-18 18:29:05eric.smithsetfiles: - unnamed
2010-10-18 18:28:55eric.smithsetfiles: - unnamed
2010-10-18 18:28:46eric.smithsetfiles: - unnamed
2010-10-18 18:28:34eric.smithsetstatus: open -> closed
resolution: not a bug
2010-10-18 18:18:46Retrosetfiles: + unnamed

messages: + msg119057
2010-10-18 18:14:15georg.brandlsetnosy: + georg.brandl
messages: + msg119053
2010-10-18 17:56:43Retrosetfiles: + unnamed

messages: + msg119047
2010-10-18 17:32:05eric.smithsetmessages: + msg119045
2010-10-18 17:23:53Retrosetfiles: + unnamed

messages: + msg119042
2010-10-18 15:55:17eric.araujosetnosy: + eric.araujo

versions: - Python 2.6, Python 2.5, 3rd party, Python 3.3
2010-10-18 12:00:25eric.smithsetmessages: + msg119010
2010-10-18 11:44:14eric.smithsetmessages: + msg119008
2010-10-18 11:38:25pitrousetnosy: + rhettinger, eric.smith
2010-10-18 10:26:01Retrocreate