HI Guys,
Ive been setting up some notification and would like to see if I can change the decimal points to 0 decimals.
HI Guys,
Ive been setting up some notification and would like to see if I can change the decimal points to 0 decimals.
To fix this sort of thing I always just end up using a Function node and a bit of JavaScript.
@torchard might have another way to reduce it…
Here is what I would use:
Three examples, you can clearly see the numbers of decimals you will end up with, 2, 1 and zero decimals places.
msg.today = msg.today.toFixed(2);
msg.totalSite = msg.totalSite.toFixed(1);
msg.pounds = msg.pounds.toFixed(0);
There are other options but this is the easiest way I’m aware of.
The only thing to be aware of is that toFixed()
converts the number to a string. This is fine if you’re just putting the value into the text notification, but if you reference it somewhere else downstream that requires it to be a number, it might break.
Good point @torchard
I have not hit that very often, but have hit it…
The solution I’ve used in the past…
msg.today = Number(msg.today.toFixed(2));
msg.totalSite = Number(msg.totalSite.toFixed(1));
msg.pounds = Number(msg.pounds.toFixed(0));
Thanks Guys!!! Ill keep you guys posted just in case. But this should be an easy fix.
HI Guys, confirming, worked well. I did the Number () version, just in case.
One last thing, how can i make the number have the Thousand coma (Ex, 1,000)
Short answer: No.
Long answer: Commas are not numbers, they are strings. As soon as you add a ,
to format the number the way you want, it MUST become a string.
Middle answer: If you are just displaying the data as per your screenshot, you can throw in a ,
no problem. If you must database it or trend it, have another msg.payload that is the number with no comma and use it.
So, to put some formatting in, you have two options in JavaScript.
toLocaleString
The advantage of this command is that its simple.
The disadvantage is that Node-RED does not support this command and also different browsers support it differently. Also in the off chance that you have global users, your number might be displayed in unexpected ways.
regex
The advantage of this is that it works in Node-RED and it will format the number (string) exactly the way you want.
The disadvantage of it is, well, regex…
msg.payload = msg.payload.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");