js & float number bug

JS浮点数精度陷阱
本文探讨了JavaScript中处理浮点数的精度问题,强调前端应避免直接进行浮点数运算,以防精度丢失。建议后端处理数值型数据并以字符串形式返回,以确保前端显示的准确性。

js & float number bug

前端最好不要处理任何的 float number 的计算/精确度转换的操作,不热很容易丢失精度,显示错误!

740516-20190311151006236-1923019089.png

前端显示个 0.0 都很费劲,最好的方式就是数值型的数据后端全部给字符串

740516-20190311144543150-532575671.png



labelWeight = 0;
if(labelWeight === 0) {
    labelWeight = labelWeight + `.0`;
} else {
    if (!labelWeight) {
        labelWeight = "";
    }
}
"0.0"
labelWeight = null;
if(labelWeight === 0) {
    labelWeight = labelWeight + `.0`;
} else {
    if (!labelWeight) {
        labelWeight = "";
    }
}
""
labelWeight = "";
if(labelWeight === 0) {
    labelWeight = labelWeight + `.0`;
} else {
    if (!labelWeight) {
        labelWeight = "";
    }
}
""
labelWeight = "0.0";
if(labelWeight === 0) {
    labelWeight = labelWeight + `.0`;
} else {
    if (!labelWeight) {
        labelWeight = "";
    }
}
undefined
labelWeight = "0.999";
if(labelWeight === 0) {
    labelWeight = labelWeight + `.0`;
} else {
    if (!labelWeight) {
        labelWeight = "";
    }
}
undefined
labelWeight;
"0.999"

740516-20190311144247068-1609924112.png

solution

后端返回处理后的 String 数据

转载于:https://www.cnblogs.com/xgqfrms/p/10510489.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值