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.
This commit is contained in:
Antoine Pitrou 2014-10-23 22:52:31 +02:00
parent fce60eaf15
commit b64bca9852
4 changed files with 75 additions and 6 deletions

View file

@ -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 atoi(str):
def atof(string, func=float):
"Parses a string as a float according to the locale settings."
return func(delocalize(string))
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, "")