function tofixed(n, d) {
if (d <= 0) {
return n;
} else {
let radix = 10 ** d;
let temp = Math.round(n * radix) / radix;
console.log(temp, "temp")
return temp;
}
}
function formatFloat(src, pos) {
return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
}
var a=30.434782608695656
var b=tofixed(a,3)
var c=formatFloat(b,2)
c=30.44
JS四舍五入问题,保留两位数
最新推荐文章于 2023-05-05 16:32:06 发布