本篇文章记录一下 微信小程序、js保留两位小数方法
直接引用下面的方法就可以了
//四舍五入并保留指定小数
// num: 数字
// n : 小数位数
function round(num,n){
return Math.round((num*Math.pow(10,n)).toFixed(1))/Math.pow(10,n);
}
//不舍全入并保留指定小数
function ceil(num,n){
return Math.ceil((num*Math.pow(10,n)).toFixed(1))/Math.pow(10,n);
}
//全舍不入并保留指定小数
function floor(num,n){
return parseInt(num*Math.pow(10,n))/Math.pow(10,n)
}
相关文章