# How to split decimals on a value

**URL:** <https://discourse.nodered.org/t/how-to-split-decimals-on-a-value/89468>\
**Category:** General\
**Created:** [13 July 2024 20:59 UTC](https://discourse.nodered.org/t/how-to-split-decimals-on-a-value/89468 "2024-07-13T20:59:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![salevilainponey](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@salevilainponey](https://discourse.nodered.org/u/salevilainponey)\
**Post date:** [13 July 2024 20:59 UTC](https://discourse.nodered.org/t/how-to-split-decimals-on-a-value/89468/1 "2024-07-13T20:59:02Z")

</div>

Hello,

I try to publish on a screen my electricity production but with the sensor that i use, i have on the screen a too big number with 2 decimals, for example 1835,48 W. I would like to split the decimals to show on the screen only 1835 W. I don't know how to do.. Does anybody know how i have to edit my script ?

var value = msg.payload;  
var unit = msg.data.attributes.unit\_of\_measurement;  
[//msg.payload](https://msg.payload) = price;

msg.payload = {  
// "text": value + ' W',  
"text": value + ' ' + unit,  
"icon": 42453,  
"duration": 4,  
"repeat": 1,  
"pushIcon": 3  
};

node.status({ fill: 'green', shape: 'ring', text: value + ' ' + unit});

return msg;

---

<div class="post-metadata">

**Author:** ![GC1](https://avatars.discourse-cdn.com/v4/letter/g/ce7236/32.png) [@GC1](https://discourse.nodered.org/u/GC1)\
**Post date:** [13 July 2024 21:14 UTC](https://discourse.nodered.org/t/how-to-split-decimals-on-a-value/89468/2 "2024-07-13T21:14:39Z")

</div>

I'm sure there are different ways of doing this but this is the way I do it. I add this line right before the return msg;  
msg.payload = Math.trunc(msg.payload);

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [13 July 2024 21:22 UTC](https://discourse.nodered.org/t/how-to-split-decimals-on-a-value/89468/3 "2024-07-13T21:22:05Z")

</div>

Could also use this to round down...  
`let value = Math.floor(msg.payload);`

Or this to round up...  
`let value = Math.ceil(msg.payload);`

Or this to round to nearest integer...  
`let value = Math.round(msg.payload);`

Or this to convert a string to an integer, also works on numbers...  
`let value = parseInt(msg.payload);`

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [14 July 2024 08:28 UTC](https://discourse.nodered.org/t/how-to-split-decimals-on-a-value/89468/4 "2024-07-14T08:28:41Z")

</div>

Also see this previous post -

> [@Problems using '.toFixed()'](https://discourse.nodered.org/t/problems-using-tofixed/4192/14):
>
> If for example, you are displaying the value in a dashboard text node, you can always use angular filters to format the number. Adding {{msg.payload | number:1}} mph to the text node value format box will format a msg.payload from 18.31158 to 18.3 mph

---

<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:** [12 September 2024 08:29 UTC](https://discourse.nodered.org/t/how-to-split-decimals-on-a-value/89468/5 "2024-09-12T08:29:03Z")

</div>

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