How can I convert a string with dot and comma into a float in Python

PythonType Conversion

Python Problem Overview


How can I convert a string like 123,456.908 to float 123456.908 in Python?

Python Solutions


Solution 1 - Python

... 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

Solution 2 - Python

Just remove the , with replace():

float("123,456.908".replace(',',''))

Solution 3 - Python

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

Solution 4 - Python

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

Solution 5 - Python

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(',', ''))

Solution 6 - Python

s =  "123,456.908"
print float(s.replace(',', ''))

Solution 7 - Python

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))

Solution 8 - Python

Here's a simple way I wrote up for you. :)

>>> number = '123,456,789.908'.replace(',', '') # '123456789.908'
>>> float(number)
123456789.908

Solution 9 - Python

Just replace, with replace().

f = float("123,456.908".replace(',','')) print(type(f))

type() will show you that it has converted into a float

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkevinluView Question on Stackoverflow
Solution 1 - PythonKarl KnechtelView Answer on Stackoverflow
Solution 2 - PythonzeekayView Answer on Stackoverflow
Solution 3 - PythonhayjView Answer on Stackoverflow
Solution 4 - PythonLuca Di LielloView Answer on Stackoverflow
Solution 5 - PythonAufwindView Answer on Stackoverflow
Solution 6 - PythonAndreas JungView Answer on Stackoverflow
Solution 7 - PythonQuincy HsiehView Answer on Stackoverflow
Solution 8 - PythonJohn DoeView Answer on Stackoverflow
Solution 9 - PythonVibhor KarnawatView Answer on Stackoverflow