Meteor, mongodb, spacebars, how do I display only 2 decimal places

I have a collection that has values ​​such as { "pctFail" : "0.3515500159795462" }, and when I pass this to the template and display as {{myTemplate}}%, it appears in my html as 0.3515500159795462%. How to display this as 0.35%?

+2
source share
3 answers

You can override the data context property using the template helper method:

Template.myTemplate.helpers({
  pctFail: function () { return this.pctFail.toFixed(2); }
})

And then use {{pctFail}}%as before. If you insist on storing the numerical property as a string, you need to return something like that instead parseFloat(this.pctFail).toFixed(2).

+7
source

, http://docs.meteor.com/#/full/template_registerhelper, , :

Template.registerHelper('toFixed', function (x, decimals) {
    return x.toFixed(decimals);
})

:

 {{toFixed item.pctFail 2}}

, -

parseFloat(x).toFixed(decimals)

.

+1

You can do something like this using substrings

Template,myTemplate.helpers({
    pctFail: function () {
        return this.pctFail.substring(0, 4);
    }
)};
0
source

Source: https://habr.com/ru/post/1688468/


All Articles