// js小数加减乘除时精度修正
export const floatObj = function () {
//加法
function add(a,b){
return math.number(math.add(math.bignumber(a), math.bignumber(b))) ;
};
//减法
function subtract(a,b){
return math.number(math.subtract(math.bignumber(a), math.bignumber(b)));
};
// 乘法
function multiply(a,b){
return math.number(math.multiply(math.bignumber(a), math.bignumber(b)));
};
// 除法
function divide(a,b){
let num
a==0||b==0? num = 0 : num = math.number(math.divide(math.bignumber(a), math.bignumber(b)));
return num
};
//四舍五入并保留n位小数(isRound是否四舍五入)
function fomatFloat(num,n,isRound){
var f = parseFloat(num);
if(isNaN(f)){
return false;
}
f = Math.round(num*Math.pow(10, isRound?n:n+1))/Math.pow(10, isRound?n:n+1); // n 幂
var s = f.toString();
var rs = s.indexOf('.');
//判定如果是整数,增加小数点再补0
if(rs < 0){
rs = s.length;
s += '.';
}
while(s.length <= rs + n){
s += '0';
}
let _s = s.split(".")[0] + '.' + (s.split(".")[1].substr(0,n));
return Number(isRound?s:_s);
}
return {
add: add,
subtract: subtract,
multiply: multiply,
divide: divide,
fomatFloat
}
}();