# Round the number with change node

**URL:** <https://discourse.nodered.org/t/round-the-number-with-change-node/73198>\
**Category:** General\
**Created:** [5 January 2023 04:12 UTC](https://discourse.nodered.org/t/round-the-number-with-change-node/73198 "2023-01-05T04:12:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jhankhana](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jhankhana](https://discourse.nodered.org/u/Jhankhana)\
**Post date:** [5 January 2023 04:12 UTC](https://discourse.nodered.org/t/round-the-number-with-change-node/73198/1 "2023-01-05T04:12:28Z")

</div>

I am trying to round msg.payload[0] to 2 decimals in change node. I have written $formatNumber(($.payload[0], '0.00'), which is not working.  
Please help me to correct it.

Edit:  
I tried {{msg.payload | number: 2}} also insted of {{msg.payload}} in value format in text node. didn't work.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [5 January 2023 04:46 UTC](https://discourse.nodered.org/t/round-the-number-with-change-node/73198/2 "2023-01-05T04:46:08Z")

</div>

Hello .. have you tried the jsonata's [$round](https://docs.jsonata.org/numeric-functions#round) function instead ?

Example `$round(123.456, 2)` results to `123.46`

In your case `$round(payload[0], 2)`

---

<div class="post-metadata">

**Author:** ![Jhankhana](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jhankhana](https://discourse.nodered.org/u/Jhankhana)\
**Post date:** [5 January 2023 04:47 UTC](https://discourse.nodered.org/t/round-the-number-with-change-node/73198/3 "2023-01-05T04:47:24Z")

</div>

yes, I tried this also but didn't work

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [5 January 2023 04:52 UTC](https://discourse.nodered.org/t/round-the-number-with-change-node/73198/4 "2023-01-05T04:52:52Z")

</div>

Here is a simple example flow that shows how $round works

```auto
[{"id":"9ba1a622916ba7d1","type":"debug","z":"54efb553244c241f","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":1940,"wires":[]},{"id":"b06261980c33dd4d","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"123.4567","payloadType":"num","x":240,"y":1940,"wires":[["dd9bf3072f747a56","85b2b0fc0627bccf"]]},{"id":"dd9bf3072f747a56","type":"change","z":"54efb553244c241f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round(payload,2)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":1940,"wires":[["9ba1a622916ba7d1"]]},{"id":"85b2b0fc0627bccf","type":"debug","z":"54efb553244c241f","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":370,"y":1860,"wires":[]}]

```

Dont know why it doesnt work in your case .. maybe your msg.payload[0] is not a number but a string ?  
Can you create a simple flow with with sample data demonstrating what doesnt work ?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [5 January 2023 12:22 UTC](https://discourse.nodered.org/t/round-the-number-with-change-node/73198/5 "2023-01-05T12:22:50Z")

</div>

and if the input is a string, or might sometimes be a string, you can try wrapping it in $number.

```auto
$round( $number(payload[0]), 2 )

```

---

<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:** [6 March 2023 12:23 UTC](https://discourse.nodered.org/t/round-the-number-with-change-node/73198/6 "2023-03-06T12:23:09Z")

</div>

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