今天在做项目时,需要在页面进行计算,但是当两个数都是小数时,计算的结果却不是想象中的:
比如1371.3-0.9算出来却是1370.39999999,后来上网搜一下,原来js是弱类型语言,没有那么高的精度计算,
所以使用下面方法解决:
render:function(data,type,full,meta) {
var returnVal = '';
var result = '';
var chargedWeight = full.chargedWeight;
var pieceNumber =full.pieceNumber;
var m = Math.pow(10,2);
if (pieceNumber == 0 && chargedWeight > 0) {
result =full.transitWeightMoney - full.deductionMoney + full.subsidyTransitMoney;
} else {
result =full.transitMoney - full.deductionMoney + full.subsidyTransitMoney;
}
returnVal = result<10?result:parseInt(result*m,10)/m ;
return returnVal; }