- min
取最小值
console.log(Math.min(10,2,0,2,2,4,5,78));
- max
取最大值
console.log(Math.max(10,2,0,2,2,4,5,78));
- ceil
向上取整
console.log(Math.ceil(12.1));
- floor
向下取整
console.log(Math.floor(12.1));
- round
四舍五入
console.log(Math.round(12.1));
console.log(Math.round(12.5));
- abs
绝对值
console.log(Math.abs(12.1));
console.log(Math.abs(-12.5));
- sqrt
开方
console.log(Math.sqrt(9));
- pow
m的n次方
console.log(Math.pow(9,2));
- random
0-1的随机数
function getRandom(n,m){
return Math.trunc(Math.random()*(m-n)+n);
}
console.log(getRandom(2,14));
- Number
转化为数字,只能处理为整数、小数
带字母的字符串不能处理,会转为NaN
console.log(Number("12.12"));//12.12
console.log(Number("12"));//12
console.log(Number("12.12aa"));//NaN
- parseInt
转化为数字,只能处理为整数
字母开头的字符串不能处理,会转为NaN
console.log(parseInt("12.12"));//12
console.log(parseInt("12"));//12
console.log(parseInt("12.12aa"));//12
console.log(parseInt("aa12.12"));//NaN
- parseFloat
转化为数字,只能处理为整数、小数
字母开头的字符串不能处理,会转为NaN
console.log(parseFloat("12.12"));//12.12
console.log(parseFloat("12"));//12
console.log(parseFloat("12.12aa"));//12.12
console.log(parseFloat("aa12.12"));//NaN
- tofixed
保留几位小数
会改变成字符串类型
const num=12.12;
console.log(num.toFixed(3))//12.120
console.log(num.toFixed(1))//12.1
- toString
转变成字符串类型
const num=12.12;
console.log(num.toString())//12.12
console.log(num.toString())//12
网友评论