0

I recieve data in string format:

input1 = ",1"
input2 = "1,"

I need to convert it into float and validate it in my tests. I expect this kind of convertion:

",1" -> 0.1
"1," -> 1.0

Is it possible without using regular expressions?

Ivan
  • 163
  • 7
  • 1
    Does this answer your question? [Formatting floats without trailing zeros](https://stackoverflow.com/questions/2440692/formatting-floats-without-trailing-zeros) – nathan liang Sep 29 '22 at 14:10
  • 1
    See [this answer](https://stackoverflow.com/a/6633912/13843268) for details (question title is otherwise misleading). – sj95126 Sep 29 '22 at 14:10

1 Answers1

3

You can substitute the , with . using replace() and then convert to float

float(input1.replace(',','.'))
imburningbabe
  • 742
  • 1
  • 2
  • 13
  • 1
    Oh I didn't expect float(".1") to be translated to 0.1. I'll have to read the documentation more carefully. – Ivan Sep 29 '22 at 14:22