diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2014-10-23 20:52:31 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2014-10-23 20:52:31 (GMT) |
commit | b64bca985283716bd86cde2876808a0b46937081 (patch) | |
tree | 2c2dac4d172cc64bf260b1cd906121123fc338a8 /Lib/locale.py | |
parent | fce60eaf150127ab7b5ab8ed2943f85706f5f67a (diff) | |
download | cpython-b64bca985283716bd86cde2876808a0b46937081.zip cpython-b64bca985283716bd86cde2876808a0b46937081.tar.gz cpython-b64bca985283716bd86cde2876808a0b46937081.tar.bz2 |
Issue #13918: Provide a locale.delocalize() function which can remove
locale-specific number formatting from a string representing a number,
without then converting it to a specific type. Patch by Cédric Krier.
Diffstat (limited to 'Lib/locale.py')
-rw-r--r-- | Lib/locale.py | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/Lib/locale.py b/Lib/locale.py index 7ff4356..6b9eb3a 100644 --- a/Lib/locale.py +++ b/Lib/locale.py @@ -301,8 +301,8 @@ def str(val): """Convert float to integer, taking the locale into account.""" return format("%.12g", val) -def atof(string, func=float): - "Parses a string as a float according to the locale settings." +def delocalize(string): + "Parses a string as a normalized number according to the locale settings." #First, get rid of the grouping ts = localeconv()['thousands_sep'] if ts: @@ -311,12 +311,15 @@ def atof(string, func=float): dd = localeconv()['decimal_point'] if dd: string = string.replace(dd, '.') - #finally, parse the string - return func(string) + return string + +def atof(string, func=float): + "Parses a string as a float according to the locale settings." + return func(delocalize(string)) -def atoi(str): +def atoi(string): "Converts a string to an integer according to the locale settings." - return atof(str, int) + return int(delocalize(string)) def _test(): setlocale(LC_ALL, "") |