Page 1 of 1

String To Float

Posted: Fri May 14, 2021 8:40 pm
by Pettefar
Hi, is there a conversion function/method to convert a string, say “0.15” to a float?

Nick
Dublin

Re: String To Float

Posted: Sat May 15, 2021 2:27 am
by taPIQoLEHUMA
float("0.15")

Re: String To Float

Posted: Sat May 15, 2021 12:51 pm
by Pettefar
Here's the section of code:
BlinkStr = str(LED[-1])
BlinkStr = BlinkStr[6:]

print(BlinkStr)
#BlinkRate = float(BlinkStr)

>>> %Run -c $EDITOR_CONTENT
0.15

If I uncomment the float line then I get an error:
>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File "<stdin>", line 88, in <module>
ValueError: invalid syntax for number

Re: String To Float

Posted: Sat May 15, 2021 1:07 pm
by SpotlightKid
Then str(LED[-2]) doesn't return what you think it does. Insert a print call to check its value and whether the substring from char 6 (zero-based) is a valid float representation - and nothing else or anything else after it.