vue封装公共⽅法处理js计算科学记数法精度问题天下九塞
声明:本⽂核⼼是很早之前参考其他⼤⽜的⽂章或博客,⽂章出处也找不到了,如有看到请留⾔,会补上出处。⾃⼰稍作改动,⼀直保留在⽤。
废话不多说,下⾯直接上在vue中⾃⼰如何使⽤的代码:
1.新建公共⽅法的js⽂件,我为这个单独建了⼀个⽂件:
calculation.js 代码:
var countDecimals = function(num) {
var len = 0;
try {
num = Number(num);
var str = String().toUpperCa();
if (str.split('E').length === 2) { // scientific notation
var isDecimal = fal;
if (str.split('.').length === 2) {
str = str.split('.')[1];
if (parInt(str.split('E')[0]) !== 0) {
isDecimal = true;
}
}
let x = str.split('E');
if (isDecimal) {
len = x[0].length;
}
len -= parInt(x[1]);
} el if (str.split('.').length === 2) { // decimal
if (parInt(str.split('.')[1]) !== 0) {
len = str.split('.')[1].length;
}
}
} catch (e) {
throw e;
} finally {
if (isNaN(len) || len < 0) {
len = 0;
}
return len;
}
};
var convertToInt = function(num) {
num = Number(num);
var newNum = num;
var times = countDecimals(num);
var temp_num = String().toUpperCa();
if (temp_num.split('E').length === 2) {
newNum = und(num * Math.pow(10, times));
} el {
newNum = Number(place(".", ""));
}
return newNum;
};
var getCorrectResult = function(type, num1, num2, result) {
var getCorrectResult = function(type, num1, num2, result) {
var temp_result = 0;
switch (type) {
ca "add":
temp_result = num1 + num2;
break;
ca "sub":
temp_result = num1 - num2;
break;
ca "div":
temp_result = num1 / num2;
break;
ca "mul":
temp_result = num1 * num2;
break;
}
if (Math.abs(result - temp_result) > 1) {
return temp_result;
}
return result;
};
export default {
//加法新手学
accAdd(num1, num2) {
num1 = Number(num1);
num2 = Number(num2);
var dec1, dec2, times;
try { dec1 = countDecimals(num1) + 1; } catch (e) { dec1 = 0; }
try { dec2 = countDecimals(num2) + 1; } catch (e) { dec2 = 0; }
times = Math.pow(10, Math.max(dec1, dec2));
// var result = (num1 * times + num2 * times) / times;
var result = (this.accMul(num1, times) + this.accMul(num2, times)) / times;
return getCorrectResult("add", num1, num2, result);
// return result;
},
//减法
accSub(num1, num2) {
num1 = Number(num1);
num2 = Number(num2);
var dec1, dec2, times;
try { dec1 = countDecimals(num1) + 1; } catch (e) { dec1 = 0; }
try { dec2 = countDecimals(num2) + 1; } catch (e) { dec2 = 0; }
times = Math.pow(10, Math.max(dec1, dec2));
// var result = Number(((num1 * times - num2 * times) / times);
var result = Number((this.accMul(num1, times) - this.accMul(num2, times)) / times); return getCorrectResult("sub", num1, num2, result);
// return result;
},
//除法
accDiv(num1, num2) {
num1 = Number(num1);
num2 = Number(num2);
var t1 = 0,
t2 = 0,
dec1, dec2;
try { t1 = countDecimals(num1); } catch (e) {}佘艳
try { t2 = countDecimals(num2); } catch (e) {}
dec1 = convertToInt(num1);
dec2 = convertToInt(num2);
霸气词语
var result = this.accMul((dec1 / dec2), Math.pow(10, t2 - t1));
return getCorrectResult("div", num1, num2, result);
// return result;
},
//乘法
国民党军衔
accMul(num1, num2) {
牡丹江大学学报num1 = Number(num1);
num1 = Number(num1);
num2 = Number(num2);十五岁的笑脸
var times = 0,
s1 = String(),
s2 = String();
try { times += countDecimals(s1); } catch (e) {}
try { times += countDecimals(s2); } catch (e) {}
var result = convertToInt(s1) * convertToInt(s2) / Math.pow(10, times); return getCorrectResult("mul", num1, num2, result);
// return result;
蓝莓的功效与作用
}
}
2.在main.js 引⼊
import cal from './utils/calculation'
Vue.prototype.cal = cal
3.组件中使⽤
//加法
this.cal.accAdd(2,1);
//减法
this.cal.accSub(2,1);
//除法
this.cal.accDiv(2,1);
//乘法
this.cal.accMul(2,1);
console.log(this.cal.accAdd(2,1)) // 3
console.log(this.cal.accSub(2,1)) // 1
console.log(this.cal.accDiv(2,1)) // 2
console.log(this.cal.accMul(2,1)) // 2