这篇文章讲述了js取整数、上取整,下取证、取余数、四舍五入、js保留2位小数并四舍五入的方法,同样也适用微信小程序,希望能帮到大家
1、丢弃小数部分,保留整数部分
parseInt(5/2) 输出结果 2
2.向上取整,有小数就整数部分加1
Math.ceil(5/2) 输出结果 3
4,向下取整,舍去小数类似于第一个
Math.floor(5/2) 输出结果 2
5、四舍五入
Math.round(2.4) Math.round(2.5) Math.round(2.6) 输出结果 2 3 3
6、四舍五入,保留指定小数
function getResult(num,n){ return Math.round((num*Math.pow(10,n)).toFixed(1))/Math.pow(10,n); } getResult(2.444,2) getResult(2.445,2) getResult(2.455,2) 输出结果 2.44 2.45 2.46
相关文章