在Javascript中如何将小数点后两位四舍五入?
我试图将一个数字四舍五入到2位小数,它给了我整数在Javascript中如何将小数点后两位四舍五入?,javascript,math,decimal,Javascript,Math,Decimal,我试图将一个数字四舍五入到2位小数,它给了我整数 var 4 = 10.99 + 89.78899999 total = number(a) + number(b); 以下是我目前拥有的: Math.round(total, 2); // this gives me 101 I need it to show 100.78 这样做的诀窍是什么?您应该使用like with: alert(12.432432.toFixed(2)); // 12.43 用于:返回四舍五入到最接近整数的数字的
var 4 = 10.99 + 89.78899999
total = number(a) + number(b);
以下是我目前拥有的:
Math.round(total, 2); // this gives me 101 I need it to show 100.78
这样做的诀窍是什么?您应该使用like with:
alert(12.432432.toFixed(2)); // 12.43
用于:返回四舍五入到最接近整数的数字的值
顺便说一句,4在javascript中不是有效的变量名,它不能以数字开头
var 4是一个无效名称为什么您认为它会给您小数点?如下所述,您可能应该使用Number.toFixed作为Math.round只接受一个参数值进行舍入,并将其舍入为最接近的整数。使用Math.round,您必须这样做才能获得两位小数。Math.roundmyNumber*100/100。谢谢您的帮助