Solution idea: Decimal into an integer before the operation. The specific code is as follows:
/**
* Addition operation, to avoid the data added after the decimal point to produce multiple digits and the loss of computational accuracy.
*
* @param num1 addend 1 | Num2 Addend 2
*/
function Numadd (NUM1, num2) {
var basenum, BaseNum1, baseNum2;
try {
BASENUM1 = Num1.tostring (). Split (".") [1].length;
} catch (e) {
BASENUM1 = 0;
}
try {
baseNum2 = Num2.tostring (). Split (".") [1].length;
} catch (e) {
baseNum2 = 0;
}
Basenum = Math.pow (Ten, Math.max (BASENUM1, baseNum2));
Return (NUM1 * basenum + num2 * basenum)/basenum;
};
/**
* Subtraction operation to avoid the loss of multi-digit and computational accuracy when the data is reduced by a few points.
*
* @param num1 minuend | Num2 meiosis
*/
function Numsub (NUM1, num2) {
var basenum, BaseNum1, baseNum2;
var precision;//accuracy
try {
BASENUM1 = Num1.tostring (). Split (".") [1].length;
} catch (e) {
BASENUM1 = 0;
}
try {
baseNum2 = Num2.tostring (). Split (".") [1].length;
} catch (e) {
baseNum2 = 0;
}
Basenum = Math.pow (Ten, Math.max (BASENUM1, baseNum2));
Precision = (baseNum1 >= baseNum2)? basenum1:basenum2;
Return ((NUM1 * basenum-num2 * basenum)/basenum). toFixed (precision);
};
/**
* Multiplication to avoid data multiplication after the decimal point resulting in multiple digits and loss of computational accuracy.
*
* @param num1 by multiplier | Num2 Multiplier
*/
function Nummulti (NUM1, num2) {
var basenum = 0;
try {
Basenum + = Num1.tostring (). Split (".") [1].length;
} catch (e) {
}
try {
Basenum + = Num2.tostring (). Split (".") [1].length;
} catch (e) {
}
Return number (num1.tostring (). Replace (".", "")) * Number (num2.tostring (). Replace (".", "") "/Math.pow (Basenum);
};
/**
* Division operation to avoid the loss of multi-digit and computational precision after dividing the decimal point of the data.
*
* @param NUM1 Dividend | NUM2 Divisor
*/
function Numdiv (NUM1, num2) {
var baseNum1 = 0, baseNum2 = 0;
var baseNum3, BaseNum4;
try {
BASENUM1 = Num1.tostring (). Split (".") [1].length;
} catch (e) {
BASENUM1 = 0;
}
try {
baseNum2 = Num2.tostring (). Split (".") [1].length;
} catch (e) {
baseNum2 = 0;
}
With (Math) {
BASENUM3 = number (num1.tostring (). Replace (".", ""));
BASENUM4 = number (num2.tostring (). Replace (".", ""));
Return (BASENUM3/BASENUM4) * POW (BASENUM2-BASENUM1);
}
};
JS decimal point loss Accuracy