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

**URL:** <https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294>\
**Category:** Developing Nodes\
**Created:** [15 October 2020 12:15 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294 "2020-10-15T12:15:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AD2TM](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@AD2TM](https://discourse.nodered.org/u/AD2TM)\
**Post date:** [15 October 2020 12:15 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/1 "2020-10-15T12:15:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 October 2020 12:23 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/2 "2020-10-15T12:23:18Z")

</div>

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [15 October 2020 12:27 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/3 "2020-10-15T12:27:58Z")

</div>

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

```auto
[{"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.

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![AD2TM](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@AD2TM](https://discourse.nodered.org/u/AD2TM)\
**Post date:** [15 October 2020 12:33 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/4 "2020-10-15T12:33:36Z")

</div>

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);

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [15 October 2020 12:35 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/5 "2020-10-15T12:35:00Z")

</div>

> [@AD2TM](#):
>
> 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.

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

```

---

<div class="post-metadata">

**Author:** ![AD2TM](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@AD2TM](https://discourse.nodered.org/u/AD2TM)\
**Post date:** [15 October 2020 12:38 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/6 "2020-10-15T12:38:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AD2TM](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@AD2TM](https://discourse.nodered.org/u/AD2TM)\
**Post date:** [15 October 2020 12:47 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/7 "2020-10-15T12:47:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![softy2k](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/softy2k/32/23707_2.png) [@softy2k](https://discourse.nodered.org/u/softy2k)\
**Post date:** [15 October 2020 14:26 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/8 "2020-10-15T14:26:44Z")

</div>

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));

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [15 October 2020 15:09 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/9 "2020-10-15T15:09:38Z")

</div>

a great reference is [www.w3schools.com](http://www.w3schools.com)  
if you google “javascript tofixed” you will get to a page devoted to that,

---

<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:** [15 October 2020 17:05 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/10 "2020-10-15T17:05:41Z")

</div>

And not forgetting the amazing resources at [Mozilla Development Network](https://developer.mozilla.org/en-US/). That has some of the best and most accurate references on JavaScript and also shows you in the [JS Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/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.

---

<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:** [14 December 2020 17:06 UTC](https://discourse.nodered.org/t/how-can-i-limit-a-simple-math-function-to-2-decimal-places/34294/11 "2020-12-14T17:06:01Z")

</div>

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