2

my question is simple.

I got my string :

a = '0,0127'

I want to convert it to a number but when i compile

float(a)

i got the following message error :

ValueError: could not convert string to float: '0,0127'

Is there another way to convert it to a number ?

Cyril P
  • 67
  • 9

2 Answers2

3

Using str.replace

Ex:

a = '0,0127'
print(float(a.replace(",", ".")))

Output:

0.0127
Rakesh
  • 81,458
  • 17
  • 76
  • 113
3

The reason this isn't working is because the decimal type only recognizes periods (.) for the decimal delimiter as this is what is common in, e.g., english. You could manually change the string or do

a = a.replace(",", ".")
float(a)

Which should work.

Auden Young
  • 1,147
  • 2
  • 18
  • 39