Round the number with change node

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.

Hello .. have you tried the jsonata's $round function instead ?

Example $round(123.456, 2) results to 123.46

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

yes, I tried this also but didn't work

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

[{"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 ?

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

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

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