String To Float

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
Pettefar
Posts: 3
Joined: Fri May 14, 2021 8:23 pm

String To Float

Post by Pettefar » Fri May 14, 2021 8:40 pm

Hi, is there a conversion function/method to convert a string, say “0.15” to a float?

Nick
Dublin

taPIQoLEHUMA
Posts: 15
Joined: Thu Mar 04, 2021 2:59 am

Re: String To Float

Post by taPIQoLEHUMA » Sat May 15, 2021 2:27 am

float("0.15")

Pettefar
Posts: 3
Joined: Fri May 14, 2021 8:23 pm

Re: String To Float

Post by Pettefar » Sat May 15, 2021 12:51 pm

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
Nick
Dublin
Unix Biker

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: String To Float

Post by SpotlightKid » Sat May 15, 2021 1:07 pm

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.

Post Reply