// 1.只保留整数部分(丢弃小数部分)
parseInt(5.1234);// 5
// 2.向下取整(<= 该数值的最大整数)和parseInt()一样
Math.floor(5.1234);// 5
// 3.向上取整(有小数,整数就+1)
Math.ceil(5.1234);
// 4.四舍五入(小数部分)
Math.round(5.1234);// 5
Math.round(5.6789);// 6
// 5.绝对值
Math.abs(-1);// 1
// 6.返回两者中的较大值
Math.max(1,2);// 2
// 7.返回两者中的较小值
Math.min(1,2);// 1
// 随机数(0-1)
Math.random();
在本例中,我们将取得介于 1 到 10 之间的一个随机数:
Math.floor((Math.random()*10)+1);//10关于Math.floor()与parseInt():它们两个只保留整数部分,但是在转换时,可能出现不精确的情况:
当16位小数,且最后一位小数为5时,取的值是该数值的最大整数;
Math.floor(5.9999999999999995);//5
当16位小数,且最后一位小数为6时,取的值是该数值的最大整数+1;
Math.floor(5.9999999999999996);//6