Math方法
- Math.abs() 去绝对值
- Math.floor() 向下取整
- Math.ceil() 向上取整
- Math.max() 取一组数据的最大值
- Math.min() 取一组数据的最小值
- Math.random() 取一个 0-1 的随机数(取不到 1)
- Math.round() 四舍五入 -- 整数
- Math.pow() 取幂次方
- Math.sqrt() 开平方
保留两位小数
最后的 0 会被省略,如果显示,只能用 toFixed(X)
const a = 3.1929840123884
const res = Math.round(a * 100) / 100
console.log(res)
const a = 3.1921840123884
const res = Math.round(a * 100) / 100
console.log(res)
const fn = (num, figures) => {
const a = Math.pow(10, figures || 2)
console.log(a)
return Math.round(num * a) / a
}
console.log(fn(3.129999, 4))