toFixed 可以做到四舍五入吗

toFixed可以做到准确的“四舍六入”,末位是5时可能会出现差错

(1.235).toFixed(2)  //1.24  正确
(1.355).toFixed(2) //1.35   错误

可以使用Math.round()将数字放大一定倍数后处理

function round(number, precision) {
    return Math.round(+number + 'e' + precision) / Math.pow(10, precision);
}

round(1.235, 2) //1.24
round(1.355, 2) //1.36
原文地址:https://www.cnblogs.com/lianglanlan/p/14191101.html