javascript数据类型:数值方法

1 数值方法

1.1 Math.abs()

// 绝对值:Math.abs(x)
Math.abs(5);//5
Math.abs(-5);//5

1.2 Math.round()

/* 四舍五入:Math.round(x) */
Math.round(1.1);//1
Math.round(1.9);//2
//经实验发现Math.round(负数)时是五舍六入;

1.3 Math.ceil()

/* 向上取整:Math.ceil(x) */
Math.ceil(1.1);//2
Math.ceil(1.9);//2

1.4 Math.floor()

/* 向下取整:Math.floor(x) */
Math.floor(1.1);//1
Math.floor(1.9);//1

1.5 Math.max() - Math.min()

/* 最大值:Math.max([value1[,value2[,...]]]) */
Math.max(1,2,3,4);//4
Math.max(-1,-2,-3);//-1

/* 最小值:Math.min([value1[,value2[,...]]]) */
Math.min(1,2,3,4);//1
Math.min(-1,-2,-3);//-3

1.6 Math.random()

/* 随机值:Math.random() */
Math.random();//0<=Math.random()<1;0.962838234971341034034137403847137487348718374

1.7 Math.trunc()

/*去除小数部分*/
Math.trunc(3.8);//3

1.8 其他方法

Math.cos(弧度);//余弦值
Math.exp(x);//e次方
Math.log(x);//幂数
Math.sqrt(x);//平方根
Math.pow(x,y);//x的y次方
Math.cbrt(8);//2 开立方
Math.sign(3)//1 正数
Math.sign(0);//0 零
Math.sign(-1);//-1 负数
Math.sign("abc");//NaN

Math.acosh(x); // 返回 x 的反双曲余弦。
Math.asinh(x); // 返回 x 的反双曲正弦。
Math.atanh(x); // 返回 x 的反双曲正切。
Math.clz32(x); // 返回 x 的 32 位二进制整数表示形式的前导 0 的个数。
Math.sinh(x); // 返回x的双曲正弦。
Math.cosh(x); // 返回 x 的双曲余弦。
Math.expm1(x); // 返回 eˆx - 1。
Math.fround(x); // 返回 x 的单精度浮点数形式。
Math.hypot(...values); // 返回所有参数的平方和的平方根。
Math.imul(x, y); // 返回两个参数以 32 位整数形式相乘的结果。
Math.log1p(x); // 返回 1 + x 的自然对数。
Math.log10(x); // 返回以 10 为底的x的对数。
Math.log2(x); // 返回以 2 为底的 x 的对数。
Math.tanh(x; //) 返回 x 的双曲正切。

2 数值类型的转换

parseInt,parseFloat已经转移到Number对象上了

/* 字符串数值化:Number(value) */
Number('100.1');//100.1
Number('12.4b5');//NaN
Number('www');//NaN

/* 保留n位小数点 num.toFixed(digits) */
(100.123).toFixed(2);//"100.12"
(100.123).toFixed(0);//"100"
posted @ 2021-01-16 10:25  格一  阅读(172)  评论(0编辑  收藏  举报