Javascript 我如何将十进制格式设置为小胡子中的货币格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11536638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can i format decimal to currency format in mustache?
提问by brainydexter
I'm using mustache to render json data received through Ajax.
我正在使用 mustache 来呈现通过 Ajax 接收到的 json 数据。
I want to render this number in currency format:
我想以货币格式呈现这个数字:
{{price}}
{{price}}
e.g.: 12300
例如:12300
How can I render it as:
我如何将其呈现为:
"12,300"
"12,300"
using mustache templating engine ?
使用 mustache 模板引擎?
回答by Otto Allmendinger
Mustache is very minimalistic and doesn't provide any support for this.
Mustache 非常简约,并且不为此提供任何支持。
You either have to supply the formatted data or a function returning the formatted data
您必须提供格式化数据或返回格式化数据的函数
{ priceDecimal: function () { return formatDecimal(this.price); } }
Another alternative is to use handlebars.jswhich supports helper functions
另一种选择是使用支持辅助函数的handlebars.js
Handlebars.registerHelper('decimal', function(number) {
return formatDecimal(number);
});
and use it like this
并像这样使用它
{{decimal price}}
回答by maxbeatty
While I agree with Lucero's answer, it is possible to use a function in Mustacheto format your data.
虽然我同意Lucero 的回答,但可以使用Mustache 中的函数来格式化数据。
Simple template:
简单模板:
<ul>
{{#price}}
<li>{{commaFormat}}</li>
{{/price}}
</ul>
JavaScript to process the data with a formatting function for your prices:
JavaScript 使用格式化函数为您的价格处理数据:
var tpl = $('#tpl').html(),
data = {
price: ['1234', '123', '123456', '1234567890'],
commaFormat: function() {
// adapted from https://stackoverflow.com/questions/2901102/how-to-print-number-with-commas-as-thousands-separators-in-javascript
return this.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
},
html = Mustache.to_html(tpl, data);
document.write(html);?
Resulting HTML:
结果 HTML:
<ul>
<li>1,234</li>
<li>123</li>
<li>123,456</li>
<li>1,234,567,890</li>
</ul>
Here's the working jsFiddleto inspect and play with further.
这是工作中的 jsFiddle,可以进一步检查和使用。
回答by JVitela
I have created a small extension for Mustache.js which enables the use of formatters inside of expressions, like {{expression | formatter}}
我为 Mustache.js 创建了一个小扩展,它允许在表达式中使用格式化程序,例如 {{expression | 格式化程序}}
You can check it in github: http://jvitela.github.io/mustache-wax/
您可以在 github 中查看:http: //jvitela.github.io/mustache-wax/
回答by Lucero
You should do that before you pass your model into mustache (or any template engine really). Apart from being able to do string operationd, the issue is that not all countries format the numbers the same, so that the display representation may differ and shouldn't be hardcoded in the template.
在将模型传递给 mustache(或任何模板引擎)之前,您应该这样做。除了能够进行字符串操作外,问题在于并非所有国家/地区的数字格式都相同,因此显示表示可能不同,不应在模板中进行硬编码。
回答by Dan King
I use the Handlebars NumeralHelper package from https://www.npmjs.com/package/handlebars.numeral
我使用来自https://www.npmjs.com/package/handlebars.numeral的 Handlebars NumeralHelper 包
First, install (and optionally save it to your package.json):
首先,安装(并可选择将其保存到您的 package.json):
npm install --save handlebars.numeral
Next, load the helper:
接下来,加载助手:
var Handlebars = require('handlebars');
var NumeralHelper = require("handlebars.numeral");
NumeralHelper.registerHelpers(Handlebars);
Finally, use it in your page:
最后,在您的页面中使用它:
<script>
myNumber = 12300:
</script>
<div>
{{ number myNumber }}
</div>
The output for this example is 12,300
.
此示例的输出为12,300
.