Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中如何将小数点后两位四舍五入?_Javascript_Math_Decimal - Fatal编程技术网

在Javascript中如何将小数点后两位四舍五入?

在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 用于:返回四舍五入到最接近整数的数字的

我试图将一个数字四舍五入到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
用于:返回四舍五入到最接近整数的数字的值

顺便说一句,4在javascript中不是有效的变量名,它不能以数字开头


var 4是一个无效名称为什么您认为它会给您小数点?如下所述,您可能应该使用Number.toFixed作为Math.round只接受一个参数值进行舍入,并将其舍入为最接近的整数。使用Math.round,您必须这样做才能获得两位小数。Math.roundmyNumber*100/100。谢谢您的帮助