/*js浮點(diǎn)數(shù)運(yùn)算問題解決辦法
@params type: 1乘法奈懒,2除法 3減法,4加法
@params arg1 arg2 按照表達(dá)式順序來
*/
floatCalculation(arg1, arg2, type) {
if (type == 1) {
let m = 0,
s1 = arg1.toString(),
s2 = arg2.toString();
try {
m += s1.split(".")[1].length
} catch (e) {
console.log(e)
}
try {
m += s2.split(".")[1].length
} catch (e) {
console.log(e)
}
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
} else if (type == 2) {
let t1 = 0,
t2 = 0,
r1, r2;
try {
t1 = arg1.toString().split(".")[1].length
} catch (e) {
console.log(e)
}
try {
t2 = arg2.toString().split(".")[1].length
} catch (e) {
console.log(e)
}
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
return (r1 / r2) * Math.pow(10, t2 - t1);
} else if (type == 3) {
let r1, r2, m, n;
try {
r1 = arg1.toString().split(".")[1].length
} catch (e) {
r1 = 0
}
try {
r2 = arg2.toString().split(".")[1].length
} catch (e) {
r2 = 0
}
m = Math.pow(10, Math.max(r1, r2));
//last modify by deeka
//動(dòng)態(tài)控制精度長度
n = (r1 >= r2) ? r1 : r2;
return ((arg2 * m - arg1 * m) / m).toFixed(n);
} else if (type == 4) {
let r1, r2, m;
try {
r1 = arg1.toString().split(".")[1].length
} catch (e) {
r1 = 0
}
try {
r2 = arg2.toString().split(".")[1].length
} catch (e) {
r2 = 0
}
m = Math.pow(10, Math.max(r1, r2));
return (arg1 * m + arg2 * m) / m;
}
}
另外一遍文章里面原文地址
/**
* @file 解決浮動(dòng)運(yùn)算問題宪巨,避免小數(shù)點(diǎn)后產(chǎn)生多位數(shù)和計(jì)算精度損失磷杏。
* 問題示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
*/
/**
* 把錯(cuò)誤的數(shù)據(jù)轉(zhuǎn)正
* strip(0.09999999999999998)=0.1
*/
function strip(num, precision = 12) {
return +parseFloat(num.toPrecision(precision));
}
/**
* Return digits length of a number
* @param {*number} num Input number
*/
function digitLength(num) {
// Get digit length of e
const eSplit = num.toString().split(/[eE]/);
const len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0));
return len > 0 ? len : 0;
}
/**
* 把小數(shù)轉(zhuǎn)成整數(shù)捏卓,支持科學(xué)計(jì)數(shù)法极祸。如果是小數(shù)則放大成整數(shù)
* @param {*number} num 輸入數(shù)
*/
function float2Fixed(num) {
if (num.toString().indexOf('e') === -1) {
return Number(num.toString().replace('.', ''));
}
const dLen = digitLength(num);
return dLen > 0 ? num * Math.pow(10, dLen) : num;
}
/**
* 檢測(cè)數(shù)字是否越界,如果越界給出提示
* @param {*number} num 輸入數(shù)
*/
function checkBoundary(num) {
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
console.warn(`${num} is beyond boundary when transfer to integer, the results may not be accurate`);
}
}
/**
* 精確乘法
*/
function times(num1, num2) {
const num1Changed = float2Fixed(num1);
const num2Changed = float2Fixed(num2);
const baseNum = digitLength(num1) + digitLength(num2);
const leftValue = num1Changed * num2Changed;
checkBoundary(leftValue);
return leftValue / Math.pow(10, baseNum);
}
/**
* 精確加法
*/
function plus(num1, num2) {
const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
}
/**
* 精確減法
*/
function minus(num1, num2) {
const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
}
/**
* 精確除法
*/
function divide(num1, num2) {
const num1Changed = float2Fixed(num1);
const num2Changed = float2Fixed(num2);
checkBoundary(num1Changed);
checkBoundary(num2Changed);
return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1)));
}
/**
* 四舍五入
*/
function round(num, ratio) {
const base = Math.pow(10, ratio);
return divide(Math.round(times(num, base)), base);
}
export { strip, plus, minus, times, divide, round, digitLength, float2Fixed };
export default { strip, plus, minus, times, divide, round, digitLength, float2Fixed };