js 保留两位小数不四舍五入

function customToFixed(num: string, len: number = 2) {
    num = num.toString()
    let index = num.indexOf('.')
    if (index !== -1) {
        num = num.substring(0, index + 1 + len)
    } else {
        num = num.substring(0)
    }
    return parseFloat(num).toFixed(len)
}

 

posted @ 2022-04-13 18:59  最好的年纪  阅读(407)  评论(0编辑  收藏  举报