How can I limit a simple math function to 2 decimal places

I have some temperature/humidity sensors, AM2301 and DS18B20, setup and working just fine
I "borrowed a flow I found here o0n Node Red forums. In that flow the author parsed out the temp from the humidity, In his script, I added a formula to convert the temp from centigrade to ferenheit
Sometimes the result is a repeating decimal. I would like to limit these repeating decimals to no more than two places.How can I do this?

You can use the toFixed() function to convert a number to a String with a fixed number of decimal places - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

There are multiple ways to do this, in a change node
e.g.

[{"id":"1b7bc2cc.67803d","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"33.333333333333","payloadType":"num","x":130,"y":580,"wires":[["99b851f3.561a88","47a812e0.ece794"]]},{"id":"99b851f3.561a88","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":320,"y":620,"wires":[]},{"id":"47a812e0.ece794","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$round(payload, 2)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":580,"wires":[["e28f0a7f.fabbc"]]},{"id":"e28f0a7f.fabbc","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":620,"wires":[]}]

or with a function node
e.g.

[{"id":"1b7bc2cc.67803d","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"33.333333333333","payloadType":"num","x":130,"y":580,"wires":[["99b851f3.561a88","47a812e0.ece794","34ead28c.3a592e"]]},{"id":"99b851f3.561a88","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":320,"y":620,"wires":[]},{"id":"34ead28c.3a592e","type":"function","z":"8d22ae29.7df6d","name":"","func":"msg.payload = Number(msg.payload.toFixed(2));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":560,"wires":[["e28f0a7f.fabbc"]]},{"id":"e28f0a7f.fabbc","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":620,"wires":[]}]

I am new to Java script so this is new territory for me.
Here is the script I am using:
var temp=msg.payload. DS18B20 .Temperature;

msg = {
payload:temp * 9/5 + 32
};

return msg;

So I would add the toFixed(2) after the return msg, so it would look like
return msg toFixed(2);

No, you want to apply toFixed(2) to the number itself, not the msg object you're returning.

msg.payload = (temp * 9/5 + 32).toFixed(2);
return msg;

AS I said I am a newbe Javascript, so it reads like a little Greek to me.
I will make the change and let you know how I make out
Thank You

Made t change I guess it is working, a least I am getting a display with the temp in tow decimal places.
Thanks again.

A little addition.
The toFixed() method converts a number into a string, rounding to a specified number of decimals.

msg.payload = (temp * 9/5 + 32).toFixed(2);
will be :
msg.payload = Number((temp * 9/5 + 32).toFixed(2));

a great reference is www.w3schools.com
if you google “javascript tofixed” you will get to a page devoted to that,

And not forgetting the amazing resources at Mozilla Development Network. That has some of the best and most accurate references on JavaScript and also shows you in the JS Reference what functions are available on what platforms.

w3schools is the place to get some basics but MDN is the place for a solid reference.

2 Likes