Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-134887: Add references to locale module for locale-aware number formatting references in string module docs #134888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Doc/library/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,10 @@ of a number respectively. It can be one of the following:
| | this option is not supported. |
+---------+----------------------------------------------------------+

For a locale aware separator, use the ``'n'`` presentation type instead.
For a locale-aware separator, use the ``'n'`` presentation type instead.
Note that the locale setting for numeric values must first be set using
the :mod:`locale` module, for example,
``locale.setlocale(locale.LC_NUMERIC, 'en_US')``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why refer to this here? At most it just belongs in the n section.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I was troubleshooting why this wasn't working for me, I searched this page in the docs for "locale aware" and "locale-aware" – this was the only hit, so I put this here as well to make sure that all spots a person may learn about this in the docs include all the information to use it directly. Most people won't read the entire page in the docs before using something.


.. versionchanged:: 3.1
Added the ``','`` option (see also :pep:`378`).
Expand Down Expand Up @@ -517,7 +520,10 @@ The available integer presentation types are:
+---------+----------------------------------------------------------+
| ``'n'`` | Number. This is the same as ``'d'``, except that it uses |
| | the current locale setting to insert the appropriate |
| | digit group separators. |
| | digit group separators. Note that the default locale is |
| | not the system locale, and therefore, you must set, at a |
| | minimum, the ``LC_NUMERIC`` category (see the |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't true, is it? I'm referring to the wording "must set": shouldn't it be "can set".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my machine, nothing happens if I use n, without setting LC_NUMERIC, so it is "must set" for me. I'm happy to adjust the wording if there are cases where it is set, but I haven't seen it.

FWIW, I'm seeing that the C locale seems to be very minimal and doesn't have a thousands separator. Since that is what the default is, instead of the system locale, the thousands separator won't be set until the user sets LC_NUMERIC, meaning it is "must set" for everyone. I'm new to the C locale as well though, so let me know if that isn't the case.

| | :mod:`locale` module for more information). |
+---------+----------------------------------------------------------+
| None | The same as ``'d'``. |
+---------+----------------------------------------------------------+
Expand Down Expand Up @@ -599,8 +605,11 @@ The available presentation types for :class:`float` and
+---------+----------------------------------------------------------+
| ``'n'`` | Number. This is the same as ``'g'``, except that it uses |
| | the current locale setting to insert the appropriate |
| | digit group separators |
| | for the integral part of a number. |
| | digit group separators for the integral part of a |
| | number. Note that the default locale is not the system |
| | locale, and therefore, you must set, at a minimum, the |
| | ``LC_NUMERIC`` category (see the :mod:`locale` module |
| | for more information). |
+---------+----------------------------------------------------------+
| ``'%'`` | Percentage. Multiplies the number by 100 and displays |
| | in fixed (``'f'``) format, followed by a percent sign. |
Expand Down
Loading