1.toFixed函数:用来保留小数点后的位数
(1.37).toFixed(1);
toFixed()函数括号里的值就是要保留的位数,值在0-20之间,在保留一位的同时也会进行四舍五入的计算,
这里的1.37得到的结果为1.4。
2.math.round():用于对数进行四舍五入:
实际上的math.round()应该说是四舍六入,对待0.5时要进行判断。实际情况如下:
Math.round(1.0); //值为1
Math.round(1.4); //值为1
Math.round(1.5); //值为2
Math.round(1.6); //值为2
Math.round(-1.0); //值为-1
Math.round(-1.4); //值为-1
Math.round(-1.5); //值为-1
Math.round(-1.6); //值为-2
这里注意到特殊情况:Math.round(-1.5); //值为-1:
因为Math.round()是根据+0.5,在向下取整得到的结果,这里指的是取比他小的第一个整数或者相等的整数。
-1.5+0.5即为1.0,所以输出的结果是-1。