1

I am trying to convert a string, "2,900.99", to a float. Trying float(int("2,900.99")) has not worked and float("2,900.99") is not working. The value is pulled from selenium and beautiful soup and the type of data is a string so I don't know how I can change the type of data. Thanks in advance for any answers!

Python 123
  • 37
  • 8

3 Answers3

2

Get rid of any comma as it has no meaning for the number itself.

float("2,900.99".replace(",", ""))

Results in 2900.99

Jonathan1609
  • 1,630
  • 1
  • 3
  • 19
1

You need to remove the commas first, like this:

float("2,900.99".replace(",",""))

returns 2900.99

sj95126
  • 3,658
  • 2
  • 7
  • 28
1

You need to remove the , by replacing it with nothing and then to convert the string to float.

float("2,900.99".replace(',',''))
Prophet
  • 21,440
  • 13
  • 47
  • 64