This article introduces you to precise calculation, numerical formatting, and direct introduction of js files.
The Code is as follows:
(Function (){
Var calc = {
/*
The addition function is used to obtain accurate addition results.
Note: The addition result of javascript has an error, which is obvious when two floating point numbers are added. This function returns a more accurate addition result.
Parameter: arg1: the first addition; the second addition of arg2; the number of decimal places to be retained by d (this parameter can be left blank. If this parameter is left blank, decimal places are not processed)
Call: Calc. Add (arg1, arg2, d)
Return Value: the result of adding two numbers.
*/
Add: function (arg1, arg2, d ){
Arg1 = arg1.toString (), arg2 = arg2.toString ();
Var arg1Arr = arg1.split ("."), arg2Arr = arg2.split ("."), d1 = arg1Arr. length = 2? Arg1Arr [1]: "", d2 = arg2Arr. length = 2? Arg2Arr [1]: "";
Var maxLen = Math. max (d1.length, d2.length );
Var m = Math. pow (10, maxLen );
Var result = Number (arg1 * m + arg2 * m)/m). toFixed (maxLen ));
Var d = arguments [2];
Return typeof d = "number "? Number (result). toFixed (d): result;
},
/*
Function: subtraction function, used to obtain the exact subtraction result.
Note: The function returns a more precise subtraction result.
Parameter: arg1: the first addition; the second addition of arg2; the number of decimal places to be retained by d (this parameter can be left unspecified. If this parameter is not set, decimal places are not processed.
Call: Calc. Sub (arg1, arg2)
Return Value: Result of two Subtraction
*/
Sub: function (arg1, arg2 ){
Return Calc. Add (arg1,-Number (arg2), arguments [2]);
},
/*
Function: multiplication function, used to obtain accurate multiplication results
Note: The function returns more accurate multiplication results.
Parameter: arg1: The first multiplier; arg2 the second multiplier; d: the number of decimal places to be retained (this parameter can be left blank. If this parameter is left blank, the number of decimal places is not processed)
Call: Calc. Mul (arg1, arg2)
Return Value: Result of multiplying two numbers
*/
Mul: function (arg1, arg2 ){
Var r1 = arg1.toString (), r2 = arg2.toString (), m, resultVal, d = arguments [2];
M = (r1.split (".") [1]? R1.split (".") [1]. length: 0) + (r2.split (".") [1]? R2.split (".") [1]. length: 0 );
ResultVal = Number (r1.replace (".", "") * Number (r2.replace (".", "")/Math. pow (10, m );
Return typeof d! = "Number "? Number (resultVal): Number (resultVal. toFixed (parseInt (d )));
},
/*
Function: Division function, used to obtain accurate division results.
Note: The function returns a more precise division result.
Parameter: arg1: divisor; arg2: divisor; d: the number of decimal places to be retained (this parameter can be left blank. If this parameter is left blank, the number of decimal places is not processed)
Call: Calc. Div (arg1, arg2)
Returned value: the result of dividing arg1 from arg2
*/
Div: function (arg1, arg2 ){
Var r1 = arg1.toString (), r2 = arg2.toString (), m, resultVal, d = arguments [2];
M = (r2.split (".") [1]? R2.split (".") [1]. length: 0)-(r1.split (".") [1]? R1.split (".") [1]. length: 0 );
ResultVal = Number (r1.replace (".", "")/Number (r2.replace (".", "") * Math. pow (10, m );
Return typeof d! = "Number "? Number (resultVal): Number (resultVal. toFixed (parseInt (d )));
},
/*
Round the value and format it.
@ Param num value (Number or String)
@ Param cent: Number of decimal places to be retained)
@ Param isThousand whether the Sub-location is required: 0: not required, 1: required (value type );
@ Return: a string in the format of '20140901'
@ Type String
Call: Calc. FormatNumber (num, cent, isThousand)
*/
FormatNumber: function formatNumber (num, cent, isThousand ){
Num = num. toString (). replace (/\ $ | \,/g ,'');
If (isNaN (num) // check that the input value is of the numerical type.
Num = "0 ";
If (isNaN (cent) // make sure that the input decimal point is a numeric value.
Cent = 0;
Cent = parseInt (cent );
Cent = Math. abs (cent); // obtain the number of decimal places and make sure it is a positive integer.
If (isNaN (isThousand) // make sure that the input must be of the numerical type in kilobytes.
IsThousand = 0;
IsThousand = parseInt (isThousand );
If (isThousand <0)
IsThousand = 0;
If (isThousand> = 1) // make sure that the input value is only 0 or 1
IsThousand = 1;
Sign = (num = Math. abs (num); // obtain the symbol (positive/negative)
// Math. floor: returns the maximum integer that is less than or equal to the value.
Num = Math. floor (num * Math. pow (10, cent) + 0.50000000001); // converts the specified decimal point to an integer first, and rounding out unnecessary decimal places.
Cents = num % Math. pow (10, cent); // obtain the decimal point.
Num = Math. floor (num/Math. pow (10, cent). toString (); // obtain the integer.
Cents = cents. toString (); // converts a decimal point to a string to evaluate the decimal point length.
While (cents. length Cents = "0" + cents;
}
If (isThousand = 0) // you do not need a kilobytes of characters.
Return (sign )? '': '-') + Num + '.' + cents );
// Format the integer in kilobytes.
For (var I = 0; I <Math. floor (num. length-(1 + I)/3); I ++)
Num = num. substring (0, num. length-(4 * I + 3) + ',' +
Num. substring (num. length-(4 * I + 3 ));
Return (sign )? '': '-') + Num + '.' + cents );
}
};
Window. Calc = calc;
}());