JavaScript计算丢失精度问题

js中计算

0.1+0.2 = 0.30000000000000004
655.4222/100.0 = 6.554221999999999
1.0-0.9 = 0.09999999999999998
65.4*100 = 6540.000000000001

解决上述问题,找到了一个最简便的方法

https://blog.csdn.net/zf18234031156/article/details/81065431

可是,

image

出现上述问题了又

如果用的vue可以参考下这个
https://github.com/nefe/number-precision

一下是修改之后的方法

var CMX = CMX || {}
/**
 ** 加
 **/
CMX.add = function (arg1, arg2) {
    arg1 = arg1 !== undefined && arg1 !== '' ? parseFloat(arg1) : 0
    arg2 = arg2 !== undefined && arg2 !== '' ? parseFloat(arg2) : 0
    var r1, r2, m, c
    try {
        r1 = arg1.toString().split(".")[1].length
    } catch (e) {
        r1 = 0
    }
    try {
        r2 = arg2.toString().split(".")[1].length
    } catch (e) {
        r2 = 0
    }
    c = Math.abs(r1 - r2)
    m = Math.pow(10, Math.max(r1, r2))
    if (c > 0) {
        var cm = Math.pow(10, c)
        if (r1 > r2) {
            arg1 = Number(arg1.toString().replace(".", ""))
            arg2 = Number(arg2.toString().replace(".", "")) * cm
        } else {
            arg1 = Number(arg1.toString().replace(".", "")) * cm
            arg2 = Number(arg2.toString().replace(".", ""))
        }
    } else {
        arg1 = Number(arg1.toString().replace(".", ""))
        arg2 = Number(arg2.toString().replace(".", ""))
    }
    return (arg1 + arg2) / m
}

/**
 ** 减
 **/
CMX.sub = function (arg1, arg2) {
    arg1 = arg1 !== undefined && arg1 !== '' ? parseFloat(arg1) : 0
    arg2 = arg2 !== undefined && arg2 !== '' ? parseFloat(arg2) : 0
    var r1, r2, m, n
    try {
        r1 = arg1.toString().split(".")[1].length
    } catch (e) {
        r1 = 0
    }
    try {
        r2 = arg2.toString().split(".")[1].length
    } catch (e) {
        r2 = 0
    }
    m = Math.pow(10, Math.max(r1, r2)) //last modify by deeka //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2
    return Number(((arg1 * m - arg2 * m) / m).toFixed(n))
}

/**
 ** 乘
 **/
CMX.mul = function (arg1, arg2) {
    arg1 = arg1 !== undefined && arg1 !== '' ? parseFloat(arg1) : 0
    arg2 = arg2 !== undefined && arg2 !== '' ? parseFloat(arg2) : 0
    if (arg1 === 0 || arg2 === 0) {
        return 0
    }
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString()
    try {
        m += s1.split(".")[1].length
    } catch (e) {
    }
    try {
        m += s2.split(".")[1].length
    } catch (e) {
    }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}

/**
 ** 除
 **/
CMX.div = function (arg1, arg2) {
    let t1 = 0, t2 = 0, t3, r1 = 0, r2 = 0, r3, r4
    if (String(arg1) === '' || String(arg2) === '' || parseFloat(arg1) === 0 || parseFloat(arg2) === 0) return 0
    try {
        // 不存在小数点
        let pointIndex1 = String(arg1).indexOf(".")
        arg1 = parseFloat(arg1)
        t1 = pointIndex1 === -1 || String(arg1).charAt(pointIndex1 + 1).length === 0 ? 0 : String(arg1).split(".")[1].length
        if (t1 !== 0) {
            r1 = String(parseFloat(arg1)).replace(".","")
        } else {
            r1 = parseFloat(arg1)
        }
    } catch (e) {
        console.error(e)
    }
    try {
        let pointIndex2 = String(arg2).indexOf(".")
        arg2 = parseFloat(arg2)
        t2 = String(arg2).indexOf(".") === -1 || String(arg2).charAt(pointIndex2 + 1).length === 0 ? 0 : String(arg2).split(".")[1].length
        if (t2 !== 0) {
            r2 = String(parseFloat(arg2)).replace(".","")
        } else {
            r2 = parseFloat(arg2)
        }
    } catch (e) {
        console.error(e)
    }
    r3 = r1 / r2
    t3 = String(r3).indexOf(".") === -1 ? 0 : String(r3).split(".")[1].length
    r4 = String(parseFloat(r3)).replace(".","")
    if (t2 > t1) {
        return (r4 * Math.pow(10, t2 - t1)) / Math.pow(10, t3)
    } else if (t2 < t1) {
        return r4 / Math.pow(10, t1 - t2 + t3)
    } else {
        return r3
    }
}

欢迎测试一下

你可能感兴趣的:(JavaScript计算丢失精度问题)