本文介绍了流星,mongodb,空格键,我如何只显示2个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合,其值类似于 { "pctFail" : "0.3515500159795462" } 并且当我将其传递给模板并显示为 {{myTemplate}}% 时,它在我的 html 中显示为0.3515500159795462%.我如何将其显示为 0.35%?

I have a collection that has values like { "pctFail" : "0.3515500159795462" } and when I pass this to a template and display as {{myTemplate}}% it displays in my html as 0.3515500159795462%. How do I display this as 0.35% ?

推荐答案

您可以使用模板辅助方法覆盖数据上下文的属性:

You could override the data context's property with a template helper method:

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

然后像以前一样使用{{pctFail}}%.如果您坚持将数字属性存储为字符串,则需要返回类似 parseFloat(this.pctFail).toFixed(2) 之类的内容.

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

这篇关于流星,mongodb,空格键,我如何只显示2个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 17:57