js中number,math相关方法

来源:互联网 发布:美军在二战的地位知乎 编辑:程序博客网 时间:2024/05/16 00:42
规范写法是引用Math,Number下相关方法,不适用全局方法
1,Number.parseInt() 取整
Number.parseInt(12.3) // 12

2,Number.parseFloat() 取浮点
Number.parseFloat(’12.33f’) //12.32

3,Number.isInteger() 判断是否为整数
Number.isInteger(12) //true
Number.isInteger(12.0)  //true

4,Math.ceil() 对一个数进行上取整。
Math.ceil(2.1)  // 3   Math.ceil(-3.2) // -3

5,Math.round() 对一个数进行四舍五入取整
Math.round(-4.5) // -4

6,Math.floor() 对一个数进行下取整

7,Math.trunc() 方法用于去除一个数的小数部分,返回整数部分
Math.trunc(3.1) //3
Math.trunc(-2.5) //-2

8,Math.sign() 判断是正数负数零
参数为正数,返回+1;参数为负数,返回-1;参数为0,返回0;
参数为-0,返回-0;其他值,返回NaN。

9,Math.max() 取最大值
Math.max(2,1,11,12,4,5) //12
Math.max(…[2,1,22,12,33]) //33

10,Math.min() 取最小值
Math.min(1,3,2,4,5,6,7,11) //1
Math.min(…[2,3,5,1,6,8]) //1