Convert string with comma to float python

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

How do you convert a string number with commas to float in Python?

To convert a string with comma separator and dot to a float: Use the str. replace[] method to remove the commas from the string. Use the float[] class to convert the string to a floating-point number.

How do you convert a string list to a float list in Python?

The most Pythonic way to convert a list of strings to a list of floats is to use the list comprehension floats = [float[x] for x in strings] . It iterates over all elements in the list and converts each list element x to a float value using the float[x] built-in function.

How do I change a comma to a dot in Python?

You can Swap commas and dots in a String using replace[] function or Using maketrans and translate[] functions in Python.

How do you remove commas in Python?

sub[] function to erase commas from the python string. The function re. sub[] is used to swap the substring. Also, it will replace any match with the other parameter, in this case, the null string, eliminating all commas from the string.

Chủ Đề