# 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:** 11\
**Page:** 2

<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 22:55 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/21 "2020-11-06T22:55:05Z")

</div>

Displaying it in a dashboard template.

---

<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:** [7 November 2020 09:42 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/22 "2020-11-07T09:42:25Z")

</div>

OK, the reason I asked was that the gauge and text nodes can do the formatting for you, but if it is in a ui\_template then you have to do it yourself, I presume.

**[Edit]** What am I talking about, In a ui\_template you can show a value to, for example, 2 decimal places using  
`<div>{{msg.payload|number:2}} </div>`

---

<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:** [7 November 2020 11:14 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/24 "2020-11-07T11:14:52Z")

</div>

> [@Colin](#):
>
> What am I talking about, In a ui\_template you can show a value to, for example, 2 decimal places using  
> `<div>{{msg.payload|number:2}} </div>`

I believe that's using an Angular filter and not Mustache.  
The OP asked;

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

But yes as per [my post above](https://discourse.nodered.org/t/problems-using-tofixed/4192/14), it can be done using Angular filter, in a ui-template node.

---

<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:** [7 November 2020 11:44 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/25 "2020-11-07T11:44:45Z")

</div>

> [@Paul-Reed](#):
>
> Are you sure Colin?

Yes. just tested it, in a ui\_template as I said. @junrau said he was using a dashboard template which I presume means ui\_template.

---

<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:** [7 November 2020 12:12 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/26 "2020-11-07T12:12:55Z")

</div>

> [@Paul-Reed](#):
>
> I believe that's using an Angular filter and not Mustache.  
> The OP asked;
> 
> > [@junrau](#):
> >
> > Is it possible to do something like this inside a Mustache template?
> 
> But yes as per [my post above](https://discourse.nodered.org/t/problems-using-tofixed/4192/14), it can be done using Angular filter, in a ui-template node.

True, so the question is, what is @junrau actually trying to do. It is a bit unfortunate we have the two types of template node, it can be confusing.

---

<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:** [7 November 2020 17:51 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/27 "2020-11-07T17:51:59Z")

</div>

I am building a SmartThings device dashboard.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/adc779b6f0917428335eb1c246c2110f25044839.png)

Here is my Mustache template:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/70bde66543fb59ca45f250eba07c4dd8943e17a8.png)

Here is my UI template:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cd7c4c77ebef980c376db315598bcc585bfc01fb.png)

I am just wondering if I can format numbers in the Mustache template since the UI template isn't doing much. All I really want to do is format the temperature with one decimal place.

---

<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:** [8 November 2020 04:19 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/30 "2020-11-08T04:19:34Z")

</div>

Thanks for the help. Turns out I really just didn't need the Mustache template. I can do all the work in the UI template, including the formatting using Angular filter like you said.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/688c0e6b27088e66e032c7303457900acfdfca5f.png)

---

<div class="post-metadata">

**Author:** ![ZyroxCo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zyroxco/32/32061_2.png) [@ZyroxCo](https://discourse.nodered.org/u/ZyroxCo)\
**Post date:** [16 November 2020 13:22 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/31 "2020-11-16T13:22:15Z")

</div>

work for me, {{value | number:1}}%, in dashboard I see XX.X%. IT was just what I need. Thanks.

---

<div class="post-metadata">

**Author:** ![apricotfarm](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@apricotfarm](https://discourse.nodered.org/u/apricotfarm)\
**Post date:** [1 March 2021 00:53 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/32 "2021-03-01T00:53:52Z")

</div>

@Hard-a-Port: Same here...this is awesome! I've been looking for this solution for some time.

---

<div class="post-metadata">

**Author:** ![delonbest](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@delonbest](https://discourse.nodered.org/u/delonbest)\
**Post date:** [26 April 2022 05:53 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/33 "2022-04-26T05:53:38Z")

</div>

> [@goncastorena](#):
>
> having problems triying to reduce the number of decimals in a sum.

It seems like Math.round() is a better solution, but it is not! In some cases it will NOT round correctly. Also, toFixed() will NOT round correctly in some cases.

To correct the rounding problem with the previous Math.round() and toFixed(), you can define a custom [JavaScript round](http://net-informations.com/js/progs/round.htm) function that performs a "nearly equal" test to determine whether a fractional value is sufficiently close to a midpoint value to be subject to midpoint rounding. The following function return the value of the given number rounded to the nearest integer accurately.

```auto
Number.prototype.roundTo = function(decimal) {
  return +(Math.round(this + "e+" + decimal) + "e-" + decimal);
}

var num = 9.7654;
console.log( num.roundTo(2)); //output 9.77

```

---

<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:** [29 April 2024 18:27 UTC](https://discourse.nodered.org/t/problems-using-tofixed/4192/34 "2024-04-29T18:27:43Z")

</div>

Closed on 29 April 2024 18:27 UTC



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