我有一个像{ "pctFail" : "0.3515500159795462" }
这样的值的集合,当我将其传递给模板并显示为{{myTemplate}}%时,它在我的html中显示为0.3515500159795462%。如何将其显示为0.35%?
答案 0 :(得分:7)
您可以使用模板助手方法覆盖数据上下文的属性:
Template.myTemplate.helpers({
pctFail: function () { return this.pctFail.toFixed(2); }
})
然后像以前一样使用{{pctFail}}%
。如果您坚持将数字属性存储为字符串,则需要返回parseFloat(this.pctFail).toFixed(2)
之类的内容。
答案 1 :(得分:1)
您也可以使用帮助函数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)
代替。
答案 2 :(得分:0)
你可以使用子串
做这样的事情Template,myTemplate.helpers({
pctFail: function () {
return this.pctFail.substring(0, 4);
}
)};