Last updated: Apr 9, 2024
Reading timeยท3 min
To convert a string with a comma separator and dot to a float:
locale.setlocale()
method to set the locale to en_US.UTF-8
.locale.atof()
method to convert the string to a float.import locale # โ Convert string with comma separator and dot to float (locale.atof()) locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') my_str = '456,789.4567' result = locale.atof(my_str) print(result) # ๐๏ธ 456789.4567
The example uses the locale.atof()
method to convert the string with a comma
and dot to a float.
We used the
locale.setlocale()
method to set the locale to en_US.UTF-8
.
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
The setlocale()
method takes the category and the locale.
The locale.LC_ALL
category sets the locale for all categories.
You can also set the locale to an empty string to use the user's preferred locale.
import locale # โ Convert string with comma separator and dot to float (locale.atof()) locale.setlocale(locale.LC_ALL, '') my_str = '456,789.4567' result = locale.atof(my_str) print(result) # ๐๏ธ 456789.4567
When the locale.LC_ALL attribute is set to an empty string, the user's preferred locale is used.
locale
parameter to.The locale.atof() method takes a string and converts it to a floating-point number.
import locale locale.setlocale(locale.LC_ALL, '') my_str = '456,789.4567' result = locale.atof(my_str) print(result) # ๐๏ธ 456789.4567
setlocale()
method before calling the locale.atof()
method.This approach would also work if the string has underscore separators instead of commas.
import locale locale.setlocale(locale.LC_ALL, '') my_str = '456_789_123.4567' result = locale.atof(my_str) print(result) # ๐๏ธ 456789123.4567
Alternatively, you can use the str.replace()
method.
This is a two-step process:
str.replace()
method to remove the commas from the string.float()
class to convert the string to a floating-point number.my_str = '456,789.4567' result = float(my_str.replace(',', '')) print(result) # ๐๏ธ 456789.4567
The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
The method doesn't change the original string. Strings are immutable in Python.
float()
class to convert it to a floating-point number.Note that in some locales a comma is used as the thousands separator (e.g. in the United States) and in some other locales a comma is used as the decimal separator (e.g. France).
If a comma is used as the decimal point, call the replace()
method twice.
my_str = '456.789,4567' result = float(my_str.replace('.', '').replace(',', '.')) print(result) # ๐๏ธ 456789.4567
The first call to the replace
method removes the period .
from the string.
The second call replaces the comma with a period.
You can learn more about the related topics by checking out the following tutorials: