Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

前台 输入数据过大的时候 传到后台变成 e 报错如何处理?

前台有个 单价*数量=总价
数据格式

{
    number,
    price,
    totalPrice
}

实际输入过程中
1.单价输入过大 出现 e
2.数量输入过大 出现 e
3.单价数量都合适 但是总价又出现 e
理想情况
三个值都不能出现 e
请问大佬 前端如何解决这种问题,可以用手动限制最大值.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

转换成字符串来操作,最后的结果输出为字符串
可以参考最下面的代码,或者使用bigInt,兼容性如图
传给后端的话,也可以传数量和单价,让后端自己计算
图片.png

bigMut(999999999, 9999999999999); // "9999999989999000000001"
function bigMut(big, common) {
    big += "";
    common += "";
    if (big.length < common.length) {
        big = [common, common = big][0];
    }
    big = big.split("").reverse();
    var oneMutManyRes = [];
    var i = 0,
    len = big.length;
    for (; i < len; i++) {
        oneMutManyRes[oneMutManyRes.length] = oneMutMany(big[i], common) + getLenZero(i);
    }
    var result = oneMutManyRes[0];
    for (i = 1, len = oneMutManyRes.length; i < len; i++) {
        result = bigNumAdd(result, oneMutManyRes[i]);
    }
    return result;
}
function getLenZero(len) {
    len += 1;
    var ary = [];
    ary.length = len;
    return ary.join("0");
}
function oneMutMany(one, many) {
    one += "";
    many += "";
    if (one.length != 1) {
        one = [many, many = one][0];
    }
    one = parseInt(one, 10);
    var i = 0,
    len = many.length,
    resAry = [],
    addTo = 0,
    curItem,
    curRes,
    toSave;
    many = many.split("").reverse();
    for (; i <= len; i++) {
        curItem = parseInt(many[i] || 0, 10);
        curRes = curItem * one + addTo;
        toSave = curRes % 10;
        addTo = (curRes - curRes % 10) / 10;
        resAry.unshift(toSave);
    }
    if (resAry[0] == 0) {
        resAry.splice(0, 1);
    }
    return resAry.join("");
}
function bigNumAdd(big, common) {
    big += "";
    common += "";
    var maxLen = Math.max(big.length, common.length),
    bAry = big.split("").reverse(),
    cAry = common.split("").reverse(),
    i = 0,
    addToNext = 0,
    resAry = [],
    fn,
    sn,
    sum;
    for (; i <= maxLen; i++) {
        fn = parseInt(bAry[i] || 0);
        sn = parseInt(cAry[i] || 0);
        sum = fn + sn + addToNext;
        addToNext = (sum - sum % 10) / 10;
        resAry.unshift(sum % 10);
    }
    if (resAry[0] == 0) {
        resAry.splice(0, 1);
    }
    return resAry.join("");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...