javascript 将数字四舍五入到精确到小数点后两位以进行货币格式设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12402273/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:06:40 来源:igfitidea点击:
Rounding a number to exactly two decimal places for currency formatting
提问by user1667474
I need to round to two decimal places for currency.
我需要为货币四舍五入到两位小数。
Both
两个都
Math.round(num*Math.pow(10,2))/Math.pow(10,2)
and
和
Math.round(num*Math.pow(10,2))/Math.pow(10,2)
work except it cuts any trailing zero's off so I get 29.9
instead of 29.90
.
工作,除了它切断任何尾随零,所以我得到29.9
而不是29.90
.
What is the best way around this?
解决此问题的最佳方法是什么?
回答by Vytalyi
You can add this to number which you want to set specific format
您可以将此添加到要设置特定格式的数字
.toFixed(2)
回答by xdazz
(Math.round(num*Math.pow(10,2))/Math.pow(10,2)).toFixed(2)