# Maths problem with function node

**URL:** <https://discourse.nodered.org/t/maths-problem-with-function-node/37503>\
**Category:** General\
**Created:** [14 December 2020 20:42 UTC](https://discourse.nodered.org/t/maths-problem-with-function-node/37503 "2020-12-14T20:42:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rich\_node](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@rich\_node](https://discourse.nodered.org/u/rich_node)\
**Post date:** [14 December 2020 20:42 UTC](https://discourse.nodered.org/t/maths-problem-with-function-node/37503/1 "2020-12-14T20:42:04Z")

</div>

I'm having problems with the function node which is not subtracting correctly, ie the below code gives the result of: 2.1899999999999977  
msg.payload=26.54-24.35;  
return msg;

Any help appreciated.

This is the flow:

[{"id":"a617c53a.3aac1","type":"inject","z":"bc64dfd3.f61d1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":1740,"wires":[["47b21e34.380cb8"]]},{"id":"47b21e34.380cb8","type":"function","z":"bc64dfd3.f61d1","name":"","func":"msg.payload=26.54-24.35;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":900,"y":1740,"wires":[["34ee6374.ca611c"]]},{"id":"34ee6374.ca611c","type":"debug","z":"bc64dfd3.f61d1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":1740,"wires":}]

---

<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:** [14 December 2020 20:51 UTC](https://discourse.nodered.org/t/maths-problem-with-function-node/37503/2 "2020-12-14T20:51:43Z")

</div>

Hi @rich_node

welcome to the forum.

You're hitting a side effect of how JavaScript represents numbers internally. This blog post provides a good explanation: [http://adripofjavascript.com/blog/drips/avoiding-problems-with-decimal-math-in-javascript.html](http://adripofjavascript.com/blog/drips/avoiding-problems-with-decimal-math-in-javascript.html)

For future reference, please have a read of this post that explains how to share code and flows in the forum: [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![rich\_node](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@rich\_node](https://discourse.nodered.org/u/rich_node)\
**Post date:** [14 December 2020 21:33 UTC](https://discourse.nodered.org/t/maths-problem-with-function-node/37503/3 "2020-12-14T21:33:27Z")

</div>

Thank you and appreciate the guidance on using this forumn.

---

<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:** [12 February 2021 21:33 UTC](https://discourse.nodered.org/t/maths-problem-with-function-node/37503/4 "2021-02-12T21:33:33Z")

</div>

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