Skip to content

bpo-34311: Add locale.localize #15275

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

Merged
merged 3 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions Doc/library/locale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ The :mod:`locale` module defines the following exception and functions:
.. versionadded:: 3.5


.. function:: localize(string, grouping=False, monetary=False)

Converts a normalized number string into a formatted string following the
:const:`LC_NUMERIC` settings.

.. versionadded:: 3.10


.. function:: atof(string)

Converts a string to a floating point number, following the :const:`LC_NUMERIC`
Expand Down
16 changes: 13 additions & 3 deletions Lib/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,14 @@ def _format(percent, value, grouping=False, monetary=False, *additional):
formatted = percent % ((value,) + additional)
else:
formatted = percent % value
if percent[-1] in 'eEfFgGdiu':
formatted = _localize(formatted, grouping, monetary)
return formatted

# Transform formatted as locale number according to the locale settings
def _localize(formatted, grouping=False, monetary=False):
# floats and decimal ints need special action!
if percent[-1] in 'eEfFgG':
if '.' in formatted:
seps = 0
parts = formatted.split('.')
if grouping:
Expand All @@ -196,7 +202,7 @@ def _format(percent, value, grouping=False, monetary=False, *additional):
formatted = decimal_point.join(parts)
if seps:
formatted = _strip_padding(formatted, seps)
elif percent[-1] in 'diu':
else:
seps = 0
if grouping:
formatted, seps = _group(formatted, monetary=monetary)
Expand Down Expand Up @@ -267,7 +273,7 @@ def currency(val, symbol=True, grouping=False, international=False):
raise ValueError("Currency formatting is not possible using "
"the 'C' locale.")

s = _format('%%.%if' % digits, abs(val), grouping, monetary=True)
s = _localize(f'{abs(val):.{digits}f}', grouping, monetary=True)
# '<' and '>' are markers if the sign must be inserted between symbol and value
s = '<' + s + '>'

Expand Down Expand Up @@ -323,6 +329,10 @@ def delocalize(string):
string = string.replace(dd, '.')
return string

def localize(string, grouping=False, monetary=False):
"""Parses a string as locale number according to the locale settings."""
return _localize(string, grouping, monetary)

def atof(string, func=float):
"Parses a string as a float according to the locale settings."
return func(delocalize(string))
Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_locale.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from decimal import Decimal
from test.support import verbose, is_android
from test.support.warnings_helper import check_warnings
import unittest
Expand Down Expand Up @@ -630,5 +631,32 @@ def test_atoi(self):
self._test_atoi('50 000', 50000)


class BaseLocalizeTest(BaseLocalizedTest):

def _test_localize(self, value, out, grouping=False):
self.assertEqual(locale.localize(value, grouping=grouping), out)


class TestEnUSLocalize(EnUSCookedTest, BaseLocalizeTest):

def test_localize(self):
self._test_localize('50000.00', '50000.00')
self._test_localize(
'{0:.16f}'.format(Decimal('1.15')), '1.1500000000000000')


class TestCLocalize(CCookedTest, BaseLocalizeTest):

def test_localize(self):
self._test_localize('50000.00', '50000.00')


class TestfrFRLocalize(FrFRCookedTest, BaseLocalizeTest):

def test_localize(self):
self._test_localize('50000.00', '50000,00')
self._test_localize('50000.00', '50 000,00', grouping=True)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Provide a locale.localize() function, which converts a normalized number string
into a locale format.