将数字四舍五入到小数点后两位以进行货币格式设置
问题描述:
我需要将货币四舍五入到小数点后两位。
I need to round to two decimal places for currency.
两者
Math.round(num*Math.pow(10,2))/Math.pow(10,2)
和
Math.round(num*Math.pow(10,2))/Math.pow(10,2)
工作,除了它会消除任何结尾的零,所以我得到 29.9
而不是 29.90
。
work except it cuts any trailing zero's off so I get 29.9
instead of 29.90
.
最好的解决方法是什么?
What is the best way around this?
答
您可以将其添加到要设置特定格式的数字中
You can add this to number which you want to set specific format
.toFixed(2)