Text to float

I have serial data in and it is the temperature (ex. 74.2) but it is text and I need to get this value into a float variable. Thanks.

.tofloat() is what you’re looking for:

local x = "74.2"; x.tofloat();

As I received my data in, I save the temperature text data to a blob

t1sin <- blob(10);

I can see t1sin in a server.log and it will print

74.22

How can I make t1sin a float.

local t1f = 0.0;
t1f = t1sin.tofloat();

does not work…

Thanks.

Can you explain what you mean by “does not work”?

Are you getting an error from .tofloat()?
Is it converting it incorrectly?

Yes, I get a red line and it says:
Fri Jul 12 2013 15:39:34 GMT-0500 (Central Daylight Time): ERROR: the index ‘tofloat’ does not exist
Fri Jul 12 2013 15:39:34 GMT-0500 (Central Daylight Time): ERROR: at readSerial:94

Oh I know what’s up.

.tofloat works on strings, but no on blobs. Try this:

t1sin <- blob(10); // read in serial data.. local t1f = t1sin.tostring().tofloat(); server.log(t1f); server.log(t1f / 10.0);

Awesome! Cool. Works…