How can I convert a string like 123,456.908
to float 123456.908
in Python?
codeforester
35.7k16 gold badges100 silver badges125 bronze badges
asked Jul 9, 2011 at 7:59
2
... Or instead of treating the commas as garbage to be filtered out, we could treat the overall string as a localized formatting of the float, and use the localization services:
from locale import atof, setlocale, LC_NUMERIC
setlocale[LC_NUMERIC, ''] # set to your default locale; for me this is
# 'English_Canada.1252'. Or you could explicitly specify a locale in which floats
# are formatted the way that you describe, if that's not how your locale works :]
atof['123,456'] # 123456.0
# To demonstrate, let's explicitly try a locale in which the comma is a
# decimal point:
setlocale[LC_NUMERIC, 'French_Canada.1252']
atof['123,456'] # 123.456
Juta
3714 silver badges12 bronze badges
answered Jul 9, 2011 at 9:22
Karl KnechtelKarl Knechtel
59k9 gold badges86 silver badges130 bronze badges
9
Just remove the ,
with replace[]
:
float["123,456.908".replace[',','']]
answered Jul 9, 2011 at 8:02
zeekayzeekay
50.7k13 gold badges107 silver badges105 bronze badges
7
If you don't know the locale
and you want to parse any kind of number, use this parseNumber[text]
function. It is not perfect but take into account most cases :
>>> parseNumber["a 125,00 €"]
125
>>> parseNumber["100.000,000"]
100000
>>> parseNumber["100 000,000"]
100000
>>> parseNumber["100,000,000"]
100000000
>>> parseNumber["100 000 000"]
100000000
>>> parseNumber["100.001 001"]
100.001
>>> parseNumber["$.3"]
0.3
>>> parseNumber[".003"]
0.003
>>> parseNumber[".003 55"]
0.003
>>> parseNumber["3 005"]
3005
>>> parseNumber["1.190,00 €"]
1190
>>> parseNumber["1190,00 €"]
1190
>>> parseNumber["1,190.00 €"]
1190
>>> parseNumber["$1190.00"]
1190
>>> parseNumber["$1 190.99"]
1190.99
>>> parseNumber["1 000 000.3"]
1000000.3
>>> parseNumber["1 0002,1.2"]
10002.1
>>> parseNumber[""]
>>> parseNumber[None]
>>> parseNumber[1]
1
>>> parseNumber[1.1]
1.1
>>> parseNumber["rrr1,.2o"]
1
>>> parseNumber["rrr ,.o"]
>>> parseNumber["rrr1rrr"]
1
answered Apr 24, 2018 at 14:15
hayjhayj
98511 silver badges21 bronze badges
1
If you have a comma as decimals separator and the dot as thousands separator, you can do:
s = s.replace['.',''].replace[',','.']
number = float[s]
Hope it will help
answered Jul 25, 2018 at 9:45
What about this?
my_string = "123,456.908"
commas_removed = my_string.replace[',', ''] # remove comma separation
my_float = float[commas_removed] # turn from string to float.
In short:
my_float = float[my_string.replace[',', '']]
answered Jul 9, 2011 at 8:03
AufwindAufwind
24.2k38 gold badges106 silver badges152 bronze badges
s = "123,456.908"
print float[s.replace[',', '']]
answered Jul 9, 2011 at 8:02
1
Better solution for different currency formats:
def text_currency_to_float[text]:
t = text
dot_pos = t.rfind['.']
comma_pos = t.rfind[',']
if comma_pos > dot_pos:
t = t.replace[".", ""]
t = t.replace[",", "."]
else:
t = t.replace[",", ""]
return[float[t]]
answered Mar 5, 2019 at 14:07
Here's a simple way I wrote up for you. :]
>>> number = '123,456,789.908'.replace[',', ''] # '123456789.908'
>>> float[number]
123456789.908
answered Jul 9, 2011 at 8:04
John DoeJohn Doe
3,3641 gold badge17 silver badges21 bronze badges
3
Just replace,
with replace[].
f = float["123,456.908".replace[',','']] print[type[f]]
type[] will show you that it has converted into a float
answered Jul 25, 2018 at 10:12
1
You may use babel:
from babel.numbers import parse_decimal
f = float[parse_decimal["123,456.908", locale="en_US"]]
answered 2 days ago
ahmkaraahmkara
3093 silver badges5 bronze badges