all Python 3localefunction
locale.atof()
numbers and money
Converts a string to a number under the current LC_NUMERIC rules: it removes the thousands separator and understands the local decimal comma.
Syntax
locale.atof(string, func=float)
Until someone calls setlocale(), the 'C' locale is in force and atof('1,5') fails just like float('1,5'): the function does not guess a Russian comma on its own. After setlocale() the parse depends on process-wide state, so for data from files an explicit s.replace(',', '.') is more reliable. The func argument lets you get a Decimal instead of a float.
Examples
import locale
from decimal import Decimal
print(locale.atof('3.14'), locale.atof('2.50', Decimal)) # → 3.14 2.50
locale.atof('1,5') # → ValueError: could not convert string to float: '1,5'
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy