|
Talk to me privately, because I want to do a project, I have a full set of calculation methods. Addition, subtraction, multiplication, division and remainder are all
Here is the code:
//According to the problem of loss of precision in addition, subtraction and remainder in floating-point calculations in js add by chengang006 at 2011-10-01
/*
* Since many times, algorithms for floating-point decimals are designed,
* In JS, only using ordinary parseFlost and the like into the envelope data type conversion will make the data lose precision
* Therefore, the method of converting to integers and then calculating is adopted
* */
//Add floating point numbers
function dcmAdd(arg1,arg2){
var 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 (accMul(arg1,m)+accMul(arg2,m))/m;
}
//Floating point subtraction add by chengang006 on 2011-10-01
/*
* The description is the same as the above addition
* */
function dcmSub(arg1,arg2){
return dcmAdd(arg1,-arg2);
}
//Floating point number takes remainder --add by chengang006 on 2011-10-02
/*
*According to actual cases, it is easy to lose accuracy. The usual practice is to expand by 10,000 times at the same time, but consider
* It's related to the previous, so we still use the first round and then calculate
* */
function dcmYu(arg1,arg2){
var 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 (accMul(arg1,m)%accMul(arg2,m))/m;
}
//For the accuracy of floating-point calculations in JavaScript, add by pengym at 2011-08-18
//Division function, used to get the exact division result
//Note: There will be errors in the division result of javascript, which will be more obvious when dividing two floating-point numbers. This function returns a more accurate division result.
//Call: accDiv(arg1,arg2)
//Return value: the exact result of dividing arg1 by arg2
function accDiv(arg1,arg2){
var t1=0,t2=0,r1,r2;
try{t1=arg1.toString().split(".")[1].length}catch(e){}
try{t2=arg2.toString().split(".")[1].length}catch(e){}
with(Math){
r1=Number(arg1.toString().replace(".",""))
r2=Number(arg2.toString().replace(".",""))
return (r1/r2)*pow(10,t2-t1);
}
}
//Multiplication function, used to get accurate multiplication result
//Note: There will be errors in the multiplication result of javascript, which will be more obvious when multiplying two floating-point numbers. This function returns a more accurate multiplication result.
//Call: accMul(arg1,arg2)
//Return value: the exact result of arg1 multiplied by arg2
function accMul(arg1,arg2){
var m=0,s1=arg1.toString(),s2=arg2.toString();
try{m+=s1.split(".")[1].length}catch(e){}
try{m+=s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}
// Converted to decimals, the original function toDecimal(datavalue) has precision problems, because it involves too much masking.
function toDecimal(datevalue){
if(datevalue.indexOf('%') != -1){
datevalue = datevalue.replace(/%/g,'');
if(datevalue.indexOf(',') != -1) {
datevalue = datevalue.replace(/,/g,'');
}
// The precision of division by 100 is increased by 2 digits on the original basis.
var decimal = (datevalue.indexOf('.') == -1)? 0: (datevalue.length-datevalue.indexOf('.')-1);
datevalue = accDiv(datevalue, 100).toFixed(decimal + 2);
// alert("toDecimal: "+ datevalue);
} else {
if(datevalue.indexOf(',') != -1){
datevalue = datevalue.replace(/,/g,'');
}
}
return datevalue;
}
// Convert decimals to percentages.
function toPercentFormat(datevalue) {
var aa = accMul(datevalue, 100);
return "" + aa + "%";
} |
|