# Return a msg.payload with more digits and decimals

**URL:** <https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526>\
**Category:** General\
**Tags:** function-node\
**Created:** [30 June 2022 05:44 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526 "2022-06-30T05:44:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zouhir](https://avatars.discourse-cdn.com/v4/letter/z/4af34b/32.png) [@zouhir](https://discourse.nodered.org/u/zouhir)\
**Post date:** [30 June 2022 05:44 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/1 "2022-06-30T05:44:19Z")

</div>

Hi, my msg.payload is returning a temperature sensor value that is supposed to be as 56,7deg, which means composed at least with 2 digits and 1 decimal.  
What should I write in the function node to get this done as I'm getting a temperature only as 5deg, see the below screenshot for more details.

 ![Need more digits and decimals](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e0db99a3c5dda64bbbc9bcaf2978fde75e47b64.jpeg)  
Thanks !

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [30 June 2022 06:03 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/2 "2022-06-30T06:03:31Z")

</div>

So the first question is ... does the sensor return decimal values? What sensor are you using?

---

<div class="post-metadata">

**Author:** ![zouhir](https://avatars.discourse-cdn.com/v4/letter/z/4af34b/32.png) [@zouhir](https://discourse.nodered.org/u/zouhir)\
**Post date:** [30 June 2022 06:08 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/3 "2022-06-30T06:08:48Z")

</div>

Yes the sensor returns decimal values, one in this case which is for the skin temperature of a motor.  
I'm using a smart sensor mounted to a motor body that measures the temperature.

---

<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:** [30 June 2022 06:42 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/4 "2022-06-30T06:42:51Z")

</div>

If the sensor is giving you the value 5, then Node-RED isn't going to be able to turn that into 56.7 without more information.

It looks like you are making an http request to get the data? Do you have any information about the API you are using? If that API isn't returning the data you want, then it sounds like any issue with the API or how you are calling it.

---

<div class="post-metadata">

**Author:** ![zouhir](https://avatars.discourse-cdn.com/v4/letter/z/4af34b/32.png) [@zouhir](https://discourse.nodered.org/u/zouhir)\
**Post date:** [30 June 2022 07:08 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/5 "2022-06-30T07:08:53Z")

</div>

The sensor is giving the value 50.0000 at the moment as you can see on the below screenshot.  
The returned value after the API is only 5, the question is how can I get XX.X in my msg.payload after my below API:

var Packdata =  
{  
"CT9.711\_SkinTemp" : msg.payload[0],  
}  
var AvevaData = {};  
AvevaData["name"]=msg.payload["name"];  
AvevaData["id"]=msg.payload["id"];  
AvevaData["timestamp"]=msg.payload["timestamp"];  
AvevaData["data"]=Packdata;

msg.payload = AvevaData;  
return msg;

 ![Last](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b1ed732b1636fe85535cc348010445684da2851d.jpeg)

---

<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:** [30 June 2022 07:37 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/6 "2022-06-30T07:37:49Z")

</div>

You keep talking about your API, but that is a function node, not an API. Is the value showing that the payload has the value "50.000" showing what is going into your function? If so then that is a string, so when you say msg.payload[0] in the function that will only the the first digit. Assuming that you want to convert that to a number from a string then you want

```auto
const PackData = {propertyname: Number(msg.payload)}

```

In future when posting code please copy/paste it here, then I could have copied the relevant code and pasted it here.  
However, I do not believe that the debug output you show comes from the function that you posted. Show us clearly what is in the message going into your function.

---

<div class="post-metadata">

**Author:** ![zouhir](https://avatars.discourse-cdn.com/v4/letter/z/4af34b/32.png) [@zouhir](https://discourse.nodered.org/u/zouhir)\
**Post date:** [30 June 2022 08:56 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/7 "2022-06-30T08:56:24Z")

</div>

The value showing that the payload has the value ''50.000'' comes from a Change Node connected to my _HTTP\_CT9.711_ request, see the below screenshot.

 ![Change Node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/9417d4bf1834d37c88494c5d977fb10a348045d7.png)

Is there anything I should add in the function node to see the value as 50.000 like a padStart() function or toFixed()?  
**"CT9.711\_SkinTemp" : msg.payload[0]**

Thanks again.

---

<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:** [30 June 2022 09:28 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/8 "2022-06-30T09:28:14Z")

</div>

> [@zouhir](#):
>
> "CT9.711\_SkinTemp" : msg.payload[0]

Change that to  
`"CT9.711_SkinTemp" : Number(msg.payload)`  
By using msg.payload[0] you are selecting the first character of the string "50.000", which is a 5.

---

<div class="post-metadata">

**Author:** ![zouhir](https://avatars.discourse-cdn.com/v4/letter/z/4af34b/32.png) [@zouhir](https://discourse.nodered.org/u/zouhir)\
**Post date:** [30 June 2022 10:26 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/9 "2022-06-30T10:26:10Z")

</div>

It finally worked with that !  
Then  
"CT9.711\_SkinTemp" : Number(msg.payload)**.toFixed(1)**  
if I want only 1 decimal.  
Perfect !

---

<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:** [30 June 2022 10:58 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/10 "2022-06-30T10:58:30Z")

</div>

I don't know what you are doing with it, but by calling toFixed() you have converted it back to a string. It is generally best to leave numbers as numbers and only convert and reduce precision when you want to display them.

---

<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:** [29 August 2022 10:58 UTC](https://discourse.nodered.org/t/return-a-msg-payload-with-more-digits-and-decimals/64526/11 "2022-08-29T10:58:57Z")

</div>

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