Convert String to Float

This is probably a simple solution. I am working with w text file and extracting some time and sensor values, some work and some don't. I have tried to compare the ones that do and as can be seen in the debug, the ones that work have 6 characters and the ones that don't have 13 characters, but they look exactly the same in the debug. The 13 character strings return NAN which indicates that the first charater is not recognised.

Any suggestions?


The first and last debug is showing that the payload is a string while the second and third are numbers. Where are you seeing the NAN?

The first and second show a successful conversion (7.6188) the last one (8.2367) returns NAN. I have stepped through each of the strings and it would appear that there are spaces ("") between show of the characters, there is certainly a space at teh beginning before teh 8 so maybe this is the problem?

Can you provide a sample of the text file that is causing the issue?
And your code that is doing the confversion.

The txt file is attached

CHWFlow.txt (1.1 KB)

and the code doing the conversion?

Sorry that's not so easy to do, the text file is from dropbox and the passes through a csv node to output an array of objects with each object representing a line of the file. This works fine and I have been able to convert the time etc, but only the col2 string cannot convert to a number. If I work through the string to check the characters at each position, there are "" (spaces?) an certain places. This may be the issue so I need to remove these from the string?

(msg.payload = msg.payload.charAt(11)
return msg;)

Object Array

So you

  • read it from dropbox - should be the same as reading it from a file on your device
  • you run it thru the CSV node
  • and then you convert it?

I'd like to see the settings for the node reading the file, the settings for the CSV node and what ever you are using to convert it.

With out that, how can I help you determine what might be happening?


Hi, thanks for the help, I have found that there are spaces (empty characters?) in the string and if I can remove these the toFloat works. These spaces were only indicated in the debug window as a larger number of characters in the string

Well (I don't understand why) you don't want to share you conversion code, I'm glad you have a solution. Good luck in the future.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.