# Problems using '.toFixed()'

**URL:** <https://discourse.nodered.org/t/problems-using-tofixed/4192>\
**Category:** General\
**Created:** [22 October 2018 14:47 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192 "2018-10-22T14:47:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![goncastorena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goncastorena/32/3284_2.png) [@goncastorena](https://discourse.nodered.org/u/goncastorena)\
**Post date:** [22 October 2018 14:47 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/1 "2018-10-22T14:47:20Z")

</div>

Hi, I'm new using Node-Red and I'm having problems triying to reduce the number of decimals in a sum.

---

<div class="post-metadata">

**Author:** ![goncastorena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goncastorena/32/3284_2.png) [@goncastorena](https://discourse.nodered.org/u/goncastorena)\
**Post date:** [22 October 2018 14:48 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/2 "2018-10-22T14:48:44Z")

</div>

Here is my code but if I only receibe messages of undefined value

var m = {payload:msg.payload.col31};  
var g = {payload:msg.payload.col32};  
var h = {payload:msg.payload.col33};  
m.topic = 'Altura 1'  
g.topic = 'Altura 2'  
h.topic = 'Altura 3'  
var sum = {payload:msg.payload.col31+msg.payload.col32+msg.payload.col33};  
sum.topic = 'suma'  
//sum = Math.round(Number(sum))  
//sum = parseFloat((sum.payload).toFixed(1));  
//sum = String(sum);  
return [m,g,h,sum];

---

<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:** [22 October 2018 14:49 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/3 "2018-10-22T14:49:51Z")

</div>

A lot will depend on the message you're passing into the function. Can you share an example so we know what it is receiving?

---

<div class="post-metadata">

**Author:** ![goncastorena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goncastorena/32/3284_2.png) [@goncastorena](https://discourse.nodered.org/u/goncastorena)\
**Post date:** [22 October 2018 14:54 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/4 "2018-10-22T14:54:43Z")

</div>

it is a message from a CSV file, but the problem is that when I made the sum of 3 colums the result have alot of decimals .

![foto1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/bcd467345517da73916df84eeb9809971ea0fbd3.png)

---

<div class="post-metadata">

**Author:** ![goncastorena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goncastorena/32/3284_2.png) [@goncastorena](https://discourse.nodered.org/u/goncastorena)\
**Post date:** [22 October 2018 14:55 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/5 "2018-10-22T14:55:16Z")

</div>

![foto2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/40850419f7c7a3f3b4e44138b5e7bafb315b8897.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 October 2018 16:09 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/6 "2018-10-22T16:09:36Z")

</div>

Unfortunately you have not shown us the contents of col31, 32, 33 which are the ones used in the function. Also show us the output of the function node which shows undefined.

Note that the commented out lines such as  
`sum = Math.round(Number(sum))`  
would not have worked because sum is a message not a number. This shows the desirability of using meaningful names such as msgsum to remind you what things are.

---

<div class="post-metadata">

**Author:** ![goncastorena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goncastorena/32/3284_2.png) [@goncastorena](https://discourse.nodered.org/u/goncastorena)\
**Post date:** [22 October 2018 16:13 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/7 "2018-10-22T16:13:22Z")

</div>

here are the values of the colums  
 ![foto3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6304082532427981d6f3c227261a6397b8c2d384.png)

---

<div class="post-metadata">

**Author:** ![goncastorena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goncastorena/32/3284_2.png) [@goncastorena](https://discourse.nodered.org/u/goncastorena)\
**Post date:** [22 October 2018 16:16 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/8 "2018-10-22T16:16:23Z")

</div>

the output is a big number, only when I try to reduce the number of decimals is when the output is undefined, probably because you said, sorry i'm pretty new here and I'm learning  
 ![foto4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4c4261832bfcedc9449e108af1199ee5fc900746.png)

---

<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:** [22 October 2018 16:21 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/9 "2018-10-22T16:21:17Z")

</div>

As Colin has said, in the code you shared, you were trying to perform those operations on `sum` which is an object, not a number. You need to operate on `sum.payload` as that is where your values are.

---

<div class="post-metadata">

**Author:** ![goncastorena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goncastorena/32/3284_2.png) [@goncastorena](https://discourse.nodered.org/u/goncastorena)\
**Post date:** [22 October 2018 16:24 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/10 "2018-10-22T16:24:46Z")

</div>

tried to use sum.payload but still receibe the message of undefined.

 ![foto5](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/cecad0a911b67f60466b28fc1532ba229aaf4876.png)

---

<div class="post-metadata">

**Author:** ![goncastorena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goncastorena/32/3284_2.png) [@goncastorena](https://discourse.nodered.org/u/goncastorena)\
**Post date:** [22 October 2018 16:49 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/11 "2018-10-22T16:49:22Z")

</div>

oooohhh forget it works perfectly, thanks for your help and patience.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 October 2018 19:04 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/12 "2018-10-22T19:04:03Z")

</div>

Just a final word, as a general rule don't worry about reducing the precision of numbers until you get to the point of actually displaying it. Usually there will be a way of specifying how you want things displayed when you get to that stage. For example, if you are using the dashboard text node then you can specify the fact that you want two digits in the node definition itself.

---

<div class="post-metadata">

**Author:** ![goncastorena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goncastorena/32/3284_2.png) [@goncastorena](https://discourse.nodered.org/u/goncastorena)\
**Post date:** [22 October 2018 19:23 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/13 "2018-10-22T19:23:43Z")

</div>

Really? how do you that? because I tried but never saw the option to trim the number long.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [22 October 2018 19:44 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/14 "2018-10-22T19:44:35Z")

</div>

If for example, you are displaying the value in a dashboard text node, you can always use angular filters to format the number.  
Adding `{{msg.payload | number:1}} mph` to the text node value format box will format a msg.payload from `18.31158` to `18.3 mph`

---

<div class="post-metadata">

**Author:** ![goncastorena](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/goncastorena/32/3284_2.png) [@goncastorena](https://discourse.nodered.org/u/goncastorena)\
**Post date:** [22 October 2018 19:47 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/15 "2018-10-22T19:47:21Z")

</div>

wow this will be very helpfull, thanks, you saved me

---

<div class="post-metadata">

**Author:** ![Hard-a-Port](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hard-a-port/32/6606_2.png) [@Hard-a-Port](https://discourse.nodered.org/u/Hard-a-Port)\
**Post date:** [4 July 2020 13:04 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/16 "2020-07-04T13:04:10Z")

</div>

Wow. I just saw this. you changed my whole world with that comment. Much appreciated!

---

<div class="post-metadata">

**Author:** ![MrEngineer1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrengineer1/32/22693_2.png) [@MrEngineer1](https://discourse.nodered.org/u/MrEngineer1)\
**Post date:** [30 July 2020 15:56 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/17 "2020-07-30T15:56:25Z")

</div>

I added this into gauge node and it works perfect.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/579878b3c855d1bacdc6c74f64c433e7c3992b5c.png)

---

<div class="post-metadata">

**Author:** ![junrau](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@junrau](https://discourse.nodered.org/u/junrau)\
**Post date:** [6 November 2020 21:14 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/18 "2020-11-06T21:14:31Z")

</div>

Is it possible to do something like this inside a Mustache template?

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [6 November 2020 21:31 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/19 "2020-11-06T21:31:11Z")

</div>

> [@junrau](#):
>
> Is it possible to do something like this inside a Mustache template?

Mustache is not made for these kinds of things, because it is intended to be logic-less.  
So no, not without ugly hacks!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 November 2020 21:35 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/20 "2020-11-06T21:35:19Z")

</div>

What are you doing with the value after the template?

[Next page](https://discourse.nodered.org/t/problems-using-tofixed/4192.md?page=2)
