如何在 JavaScript 中打印货币格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4022171/
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 do I print currency format in JavaScript
提问by KoolKabin
I have some price values to display in my page.
我有一些价格值要显示在我的页面中。
I am writing a function that takes the float price and returns the formatted currency value with currency code too.
我正在编写一个函数,它采用浮动价格并返回带有货币代码的格式化货币值。
like fnPrice(
1001.01) should print$ 1,000.01.
像 fnPrice(
1001.01) 应该打印$ 1,000.01。
回答by TungHarry
You can using code :
您可以使用代码:
function formatMoney(number) {
return number.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
}
console.log(formatMoney(10000)); // ,000.00
console.log(formatMoney(1000000)); // ,000,000.00
It was answered at Javascript Function to Format as Money
它在Javascript Function to Format as Money 中得到了回答
Or you can custom :
或者您可以自定义:
function formatMoney(number) {
return '$ '+ number.toLocaleString('en-US');
}
回答by joni
You've got to do this by hand, there is nothing builtin into JS. For an example look at this post here: How can I format numbers as money in JavaScript?
您必须手动执行此操作,JS 中没有内置任何内容。有关示例,请查看此处的这篇文章:如何在 JavaScript 中将数字格式化为货币?

