# String to number

**URL:** <https://discourse.nodered.org/t/string-to-number/988>\
**Category:** General\
**Created:** [21 June 2018 15:25 UTC](https://discourse.nodered.org/t/string-to-number/988 "2018-06-21T15:25:51Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardokemp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ricardokemp/32/743_2.png) [@ricardokemp](https://discourse.nodered.org/u/ricardokemp)\
**Post date:** [21 June 2018 15:25 UTC](https://discourse.nodered.org/t/string-to-number/988/1 "2018-06-21T15:25:51Z")

</div>

Hello  
I am reaading one information MQTT in msg.payload. But i receive one string

How i can convert it to number ?  
My intent is show this number in one graph or chart

Thank you

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [21 June 2018 15:34 UTC](https://discourse.nodered.org/t/string-to-number/988/2 "2018-06-21T15:34:00Z")

</div>

The Node-RED Function node uses JavaScript. A quick google search for how to do that will reveal lots of answers, for example:

> **[Converting strings to numbers with vanilla JavaScript](https://gomakethings.com/converting-strings-to-numbers-with-vanilla-javascript/)**
>
> In JavaScript, you can represent a number is an actual number (ex. 42), or as a string (ex. '42').
> If you were to use a strict comparison to compare the two, it would fail because they’re two different types of objects.
> var num1 = 42; var num2 =...

Nick

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 June 2018 16:13 UTC](https://discourse.nodered.org/t/string-to-number/988/3 "2018-06-21T16:13:40Z")

</div>

Or you can use a Change node and a bit of JSONata  
e.g string "1234" to 1234

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/6510e1165868c78c2e6117d3b6c84c1d30bd083d.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [21 June 2018 16:31 UTC](https://discourse.nodered.org/t/string-to-number/988/4 "2018-06-21T16:31:59Z")

</div>

though the ui\_chart node should cope with a number like string just fine…

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [21 June 2018 16:32 UTC](https://discourse.nodered.org/t/string-to-number/988/5 "2018-06-21T16:32:26Z")

</div>

remember if you are looking what comes out of an MQTT in node, if you will get a JSON string if the published message was an object. So adding a debug node to the output of the MATT in node will show you the msg type and you can see if you need/want to run it thru a ‘JSON’ node before using it (this has bit me in the past)

---

<div class="post-metadata">

**Author:** ![ricardokemp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ricardokemp/32/743_2.png) [@ricardokemp](https://discourse.nodered.org/u/ricardokemp)\
**Post date:** [21 June 2018 17:23 UTC](https://discourse.nodered.org/t/string-to-number/988/6 "2018-06-21T17:23:04Z")

</div>

Hello

As you can see, the values comming from topic Umidade (MQTT) I see as string

![Picture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/50d0f48f739e9160997cd158415780e8b778187b.png)

I need to convert to number, because the chart does not change

---

<div class="post-metadata">

**Author:** ![tilleul](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tilleul/32/78_2.png) [@tilleul](https://discourse.nodered.org/u/tilleul)\
**Post date:** [21 June 2018 17:49 UTC](https://discourse.nodered.org/t/string-to-number/988/7 "2018-06-21T17:49:45Z")

</div>

Have you tried parseInt() ...

> **[parseInt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt)**
>
> The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).

... or parseFloat() ...

> **[parseFloat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat)**
>
> The parseFloat() function parses an argument and returns a floating point number.

.... in a function node ?

---

<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:** [21 June 2018 20:31 UTC](https://discourse.nodered.org/t/string-to-number/988/8 "2018-06-21T20:31:59Z")

</div>

I am surprised that the gauge node will not work with a string, but to convert it you can use a function node containing

```auto
msg.payload = Number(msg.payload);
return msg;

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 June 2018 05:43 UTC](https://discourse.nodered.org/t/string-to-number/988/9 "2018-06-22T05:43:45Z")

</div>

Me too, will look at that. (Though to be fair the original question did say chart 🙂

---

<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:** [22 June 2018 07:23 UTC](https://discourse.nodered.org/t/string-to-number/988/10 "2018-06-22T07:23:51Z")

</div>

Sending strings to Gauge node works fine for me

```auto
[{"id":"ba62ae94.76cd78","type":"ui_gauge","z":"6dc690a3.1abc88","name":"","group":"88342af7.aabb3","order":0,"width":0,"height":0,"gtype":"gage","title":"Gauge","label":"units","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":251,"y":1335,"wires":[]},{"id":"5a7769b0.762a9","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"","payload":"20","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":85,"y":1332,"wires":[["ba62ae94.76cd78"]]},{"id":"87df3a5c.06d738","type":"inject","z":"6dc690a3.1abc88","name":"","topic":"","payload":"40","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":84,"y":1402,"wires":[["ba62ae94.76cd78"]]},{"id":"88342af7.aabb3","type":"ui_group","z":"","name":"Default","tab":"afb71d0f.e96268","disp":false,"width":"6"},{"id":"afb71d0f.e96268","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

```

@ricardokemp how have you got the gauge configured? Post a screenshot or export the flow for the Gauge node.  
Also post what you see in the node-red log when node-red is started so we can see the versions of everything. The command node-red-log should show you the log if you are not running node-red in a terminal.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 June 2018 15:56 UTC](https://discourse.nodered.org/t/string-to-number/988/11 "2018-06-22T15:56:33Z")

</div>

Indeed - simple strings do seem to work on latest.

---

<div class="post-metadata">

**Author:** ![tonylane26](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tonylane26/32/9272_2.png) [@tonylane26](https://discourse.nodered.org/u/tonylane26)\
**Post date:** [15 August 2019 16:41 UTC](https://discourse.nodered.org/t/string-to-number/988/12 "2019-08-15T16:41:53Z")

</div>

Thanks for opening my eyes to JSONata. This is easy and makes the Flow very understandable and reusable without writing a function. My initial attempt failed because the input was a string array from a web site. Changing the output to $number(payload[0]) fixed that.

---

<div class="post-metadata">

**Author:** ![CristeaPaul](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cristeapaul/32/3663_2.png) [@CristeaPaul](https://discourse.nodered.org/u/CristeaPaul)\
**Post date:** [28 September 2019 16:51 UTC](https://discourse.nodered.org/t/string-to-number/988/13 "2019-09-28T16:51:17Z")

</div>

> [@Colin](#):
>
> msg.payload = Number(msg.payload); return msg;

Thx for this, you save me 🙂

---

<div class="post-metadata">

**Author:** ![xale12](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xale12/32/20112_2.png) [@xale12](https://discourse.nodered.org/u/xale12)\
**Post date:** [2 May 2020 10:33 UTC](https://discourse.nodered.org/t/string-to-number/988/14 "2020-05-02T10:33:35Z")

</div>

my string is not only combine number , it also have word on it . like openPercent: 50 . What I should do ?

---

<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:** [2 May 2020 14:12 UTC](https://discourse.nodered.org/t/string-to-number/988/15 "2020-05-02T14:12:00Z")

</div>

Can you feed the string into a debug node and show us what the debug node shows please?

---

<div class="post-metadata">

**Author:** ![xale12](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xale12/32/20112_2.png) [@xale12](https://discourse.nodered.org/u/xale12)\
**Post date:** [2 May 2020 14:38 UTC](https://discourse.nodered.org/t/string-to-number/988/16 "2020-05-02T14:38:45Z")

</div>

I just solve my problem with a function node thank !!!

---

<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:** [2 May 2020 14:42 UTC](https://discourse.nodered.org/t/string-to-number/988/17 "2020-05-02T14:42:35Z")

</div>

There may well be a better way, I suspect that you are parsing strings instead of converting JSON to javascript objects, but without more detail I cannot be certain.

---

<div class="post-metadata">

**Author:** ![daveymg-nz](https://avatars.discourse-cdn.com/v4/letter/d/9de053/32.png) [@daveymg-nz](https://discourse.nodered.org/u/daveymg-nz)\
**Post date:** [25 May 2020 03:47 UTC](https://discourse.nodered.org/t/string-to-number/988/18 "2020-05-25T03:47:45Z")

</div>

Thanks for the Number() solution hint, solved a problem for me.

---

<div class="post-metadata">

**Author:** ![877](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/877/32/8207_2.png) [@877](https://discourse.nodered.org/u/877)\
**Post date:** [12 September 2020 14:57 UTC](https://discourse.nodered.org/t/string-to-number/988/19 "2020-09-12T14:57:31Z")

</div>

```auto
msg.payload = Number(msg.payload);
return msg;

```

Worked for me, could not get parseFloat() working for some reason. Thank you

---

<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:** [12 September 2020 15:12 UTC](https://discourse.nodered.org/t/string-to-number/988/20 "2020-09-12T15:12:54Z")

</div>

> [@877](#):
>
> could not get parseFloat() working for some reason.

What did you try?

[Next page](https://discourse.nodered.org/t/string-to-number/988.md?page=2)
