Problems using '.toFixed()'

Displaying it in a dashboard template.

1 Like

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>

1 Like

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

But yes as per my post above, it can be done using Angular filter, in a ui-template node.

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.

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.

1 Like

I am building a SmartThings device dashboard.

Here is my Mustache template:

Here is my UI template:

image

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.

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.

2 Likes

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

1 Like

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

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 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.

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