# parseFloat() return as it want value and NaN

**URL:** https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815
**Category:** General
**Created:** [18 March 2021 12:51 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815 "2021-03-18T12:51:32Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![corso66](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/corso66/32/41143_2.png) [@corso66](https://discourse.nodered.org/u/corso66)
#### Post date: [18 March 2021 12:51 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/1 "2021-03-18T12:51:33Z")

</div>

Hello everybody, thanks about this forum, it's a nice node red encyclopedia 😉

Generaly, i understand what i do, i can pass 1min, 1hour, 1days and more when i meet a problem but now, madness begins ...

Consider this function. I receive from my IoT an ASCII message like this : `"02h05m3h"`.

I juste want to convert this ascii chain to 3 numbers in an array, using `parseFloat()` function.

```auto
var o;
var raw = {};
var ascii = {};
var splited;

o = msg.payload;

//test if there is valid raw data
if(o[0] === 2) //is a conf rawdata
{
    raw.topic = "RAM";
    raw.enabled = false; //Disable button
    raw.payload = msg.payload;
    return [raw,null];
}
else if(o[0] < 1 || 2 < o[0]) //is a ascii data ?
{
    ascii.topic = "RAM";
    ascii.enabled = false; //Disable button
    ascii.payload = msg.payload;[/code]
    
    splited = String(o); //toString
    splited = splited.replace(/[a-z]/g,":");// replace all lowercase to ':'
    splited = splited.split(":"); // Split

    for(var i=0; i<splited.length; i++)
    {
        splited[i] = parseFloat(splited[i]);// convert char to number
    }
    msg.payload = splited;
    return [null,msg];
}
```

It return me :

```auto
msg.payload : array[4]
[2, 5, NaN, NaN]
```

Where is 3 number ?

However, if i send my payload just after spliting, i have this :

```auto
msg.payload : array[4]
["02", "05", "3", ""]
```

Maybe a bad type ... but what i missed ?

Thank you very much 😄

---

<div class="post-metadata">

### Author: ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)
#### Post date: [18 March 2021 13:16 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/2 "2021-03-18T13:16:48Z")

</div>

Another approach would be to use Date.parse like this: [Parse Time HH:mm:ss without date in javascript](https://techstream.org/Bits/Javascript/Javascript-Parse-time)

Edit: this function seems to work:

```auto
var regex_tx = /(\d*)h(\d*)m(\d*)h/;

const m = msg.payload.match(regex_tx);    
var res = [];
for (var n = 1; n < m.length; n++) {
 res.push(Number(m[n]));
}
msg.payload = res
return msg;

```

---

<div class="post-metadata">

### Author: ![corso66](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/corso66/32/41143_2.png) [@corso66](https://discourse.nodered.org/u/corso66)
#### Post date: [18 March 2021 13:30 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/3 "2021-03-18T13:30:56Z")

</div>

Thank you for you answer. The problem is, i can receive negative ascii value like `"-2"`, and i understand that only parseFloat() can do this convertion.

I add that i've tried also with parseInt() and Number(), it return me the same NaN at the same position 🤔

Ok i just post without seen your code 😄

---

<div class="post-metadata">

### Author: ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)
#### Post date: [18 March 2021 13:46 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/4 "2021-03-18T13:46:13Z")

</div>

Then you adjust your regexp like

```auto
var regex_tx = /(-*\d*)h(-*\d*)m(-*\d*)h/;

```

It doesn't matter whether you use Number() or parseFloat(), it gives you the "-2"

---

<div class="post-metadata">

### Author: ![corso66](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/corso66/32/41143_2.png) [@corso66](https://discourse.nodered.org/u/corso66)
#### Post date: [18 March 2021 14:05 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/5 "2021-03-18T14:05:17Z")

</div>

Ok i tested you code. Below 😄

```auto
var o;
var raw = {};
var ascii = {};

o = msg.payload;

/* test if there is valid raw data */
if(o[0] === 2) //is a conf rawdata
{
    raw.topic = "RAM";
    raw.enabled = false; //Disable button
    raw.payload = msg.payload;
    return [raw,null];
}
else if(o[0] < 1 || 2 < o[0]) //is a ascii data ?
{
    ascii.topic = "RAM";
    ascii.enabled = false; //Disable button
    ascii.payload = msg.payload;
    
    //splited = String(o);
    var regex_tx = /(\d*)h(\d*)m(\d*)h/;
    const m = msg.payload.match(regex_tx);
    var res = [];
    for (var n = 1; n < m.length; n++)
    {
        res.push(Number(m));
    }
    msg.payload = res

    return [null,msg];
}
```

I have this error :

```auto
TypeError: msg.payload.match is not a function
```

I think i must to String() before, but where ?

---

<div class="post-metadata">

### Author: ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)
#### Post date: [18 March 2021 14:11 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/6 "2021-03-18T14:11:18Z")

</div>

Almost my code, there is the index missing in the loop 😉

Instead of msg.payload you have to use your string. In my case the string was directly in msg.payload, because I used an inject node. Maybe replace

```auto
const m = msg.payload.match(regex_tx);

```

with

```auto
var SomeString = String(o);
const m = SomeString match(regex_tx);

```

But that's just a guess. Please feed your input in the debug node and show what it says

---

<div class="post-metadata">

### Author: ![corso66](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/corso66/32/41143_2.png) [@corso66](https://discourse.nodered.org/u/corso66)
#### Post date: [18 March 2021 14:30 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/7 "2021-03-18T14:30:43Z")

</div>

😵

Well, here is the input (from my device) :

```auto
buffer[10][string]
03h10m 5h
```

or

```auto
msg.payload : buffer[10]
[48, 51, 104, 49, 48, 109, 0, 0, 53, 104]
```

You can see 2 spaces ... i will to fix it now.

The fonction node code now is :

```auto
var o;
var raw = {};
var ascii = {};

o = msg.payload;

/* test if there is valid raw data */
if(o[0] === 2) //is a conf rawdata
{
    raw.topic = "RAM";
    raw.enabled = false; //Disable button
    raw.payload = msg.payload;
    return [raw,null];
}
else if(o[0] < 1 || 2 < o[0]) //is a ascii data ?
{
    ascii.topic = "RAM";
    ascii.enabled = false; //Disable button
    ascii.payload = msg.payload;

    var regex_tx = /(\d*)h(\d*)m(\d*)h/;

    var SomeString = String(o);
    const m = SomeString.match(regex_tx);    
    var res = [];
    for (var n = 1; n < m.length; n++)
    {
        res.push(Number(m[n]));
    }
    
    msg.payload = res

    return [null,msg];
}
```

And i have this error at the output :

```auto
TypeError: Cannot read property 'length' of null
```

now i am completely lost

---

<div class="post-metadata">

### Author: ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)
#### Post date: [18 March 2021 14:54 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/8 "2021-03-18T14:54:14Z")

</div>

First thing: it's a buffer, use

```auto
var SomeString = o.toString();

```

Second thing: if you have spaces in the string, your regexp should be

```auto
/\s*(-*\d*)h\s*(-*\d*)m\s*(-*\d*)h/;

```

Regexps are a wonderful thing 😁

Edit: why do you parse for floats if you apparently only have ints?

---

<div class="post-metadata">

### Author: ![corso66](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/corso66/32/41143_2.png) [@corso66](https://discourse.nodered.org/u/corso66)
#### Post date: [18 March 2021 15:26 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/9 "2021-03-18T15:26:35Z")

</div>

Ok i'm trying that.

I used parseFloat because understood that only this function convert for example `"-10"`  
But if Number do that !

---

<div class="post-metadata">

### Author: ![corso66](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/corso66/32/41143_2.png) [@corso66](https://discourse.nodered.org/u/corso66)
#### Post date: [18 March 2021 15:34 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/10 "2021-03-18T15:34:23Z")

</div>

> [@corso66](#):
>
> `TypeError: Cannot read property 'length' of null`

Ok i have change te code as you told me, i have always this error of type. Why length is a problem now ?  
Also have deleted bad space from my device...

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [18 March 2021 15:55 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/11 "2021-03-18T15:55:00Z")

</div>

> [@corso66](#):
>
> `for (var n = 1; n < m.length; n++)`

Assuming that it is that line that generating the error then it means that `m` is null.

Read the node-red docs page on Writing Functions and it will tell you how you can use `node-warn` to display values from within your function. You can add `node.warn()` statements at appropriate points to see what is going on.

---

<div class="post-metadata">

### Author: ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)
#### Post date: [18 March 2021 22:06 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/12 "2021-03-18T22:06:23Z")

</div>

As Colin said:

> [@Colin](#):
>
> Read the node-red docs page on Writing Functions and it will tell you how you can use `node-warn` to display values from within your function. You can add `node.warn()` statements at appropriate points to see what is going on.

Something with your string that you are trying to "match" is wrong, so there is no match and the array m is null. So output the string before the match and see what's wrong

---

<div class="post-metadata">

### Author: ![corso66](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/corso66/32/41143_2.png) [@corso66](https://discourse.nodered.org/u/corso66)
#### Post date: [19 March 2021 09:27 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/13 "2021-03-19T09:27:45Z")

</div>

Hi!

Yesterday i have undersatnd the problem, thanks Simon01011 to shown me the way.

My electronics card send some data to red node, some time in ASCII or RAW data. I have designed a "terminal" to display commands sent or received with a text node. But, spaces characters sent by my IoT are ignored, because those value is 0x00 (not really a space char).  
So by looking the input data, i've seen that IoT sent, for example if an ascii number `1` to send (has not hundred or ten), `001`. Or another like `-1` will viewed in debug node as `- 1`.

So, Number() can't process this last one and return un NaN. Same with parseFloat().

I keep preciously you code but it's more hard for me. So i have retryed my first code above, i have modified my firmware of my IoT and deleted excess 0x00 chars, and everythings is ok.

Thank you very much everybody 😄

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [2 April 2021 09:28 UTC](https://discourse.nodered.org/t/parsefloat-return-as-it-want-value-and-nan/42815/14 "2021-04-02T09:28:03Z")

</div>

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