Copy code code as follows:
(Function () {)
var Calc =
/*
Function, adding functions to obtain accurate results of additional method
Note: There will be errors in the results of JavaScript, and it will be obvious when the two floating -point numbers are added. This function returns a more accurate result of addition.
Parameters: ARG1: The first plus number; the second plus number of ARG2; D to retain the decimal digits (may not be passed on this parameter, if you do not pass, you do not handle the decimal digits)
Call: Calc.add (ARG1, ARG2, D)
Return value: the results of two numbers add
*/
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 = av2arr.Length == 2? Arg2arrrrrrrr [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 to obtain accurate subtraction results
Note: The function returns a more accurate subtraction result.
Parameters: ARG1: The first plus; the second plus number of ARG2; D to retain the decimal digits (may not be passed on this parameter.
Call: CALC.SUB (ARG1, ARG2)
Return value: the result of two numbers reduction
*/
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 a more accurate multiplication result.
Parameters: ARG1: The first multiplication; ARG2 second multiplication; D to retain the decimal digits (may not be passed on this parameter, if you do not pass, you do not handle the decimal digits)
Call: Calc.mul (ARG1, ARG2)
Return value: the result of the multiplication of 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: Except the method to obtain accurate removal results
Note: The function returns the more accurate removal result.
Parameters: ARG1: Division; ARG2 is divided; D must be reserved by the decimal digits (may not be passed on this parameter, if you do not pass, you do not handle the decimal digits)
Call: CALC.DIV (ARG1, ARG2)
Return value: ARG1 Except the results of 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 (".")? 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)));
},
/*
Format the value after entering the five houses.
@Param NUM value (Number or String)
@param Center to be retained
@Param Isthousand, do you need a thousand points 0: No need, 1: need (numerical type);
@Return format string, such as '1,234,567.45'
@Type String
Call: Calc.Formatnumber (NUM, Cent, ISTHOUSAND)
*/
Formatnumber: Function Formatnumber (Num, Cent, Isthousand) {
num = num.tostring (). Replace (// $ |/,/g, '');
if (isnan (num)) // Check the number of intake values for numerical types.
num = "0";
If (ISNAN (Cent)) // Make sure that the small number is introduced to the value type value.
center = 0;
Center = PARSEINT (center);
center = math.abs (center); // Find a decimal digit to ensure that it is a positive integer.
If (ISNAN (ISTHOUSAND)) // Make sure whether it is required to be passed into a thousand points into numerical types.
Isthousand = 0;
iStHousand = PARSEINT (Isthousand);
if (Isthousand <0)
Isthousand = 0;
If (Isthousand> = 1) // Make sure the intake value is only 0 or 1
Isthousand = 1;
sign = (num == (num = math.abs (num)); // Get the symbol (positive/negative)
//Math.floor: Returns the maximum integer that is less than equal to its numerical parameters
num = math.floor (num*math.pow (10, center) +0.50000000001); // convert the specified decimal bit into an integer. The extra decimal bits are four and five.
center = num%math.pow (10, center); // Find a decimal digit value.
num = math.floor (num/math.pow (10, center)). Tostring (); // Find an integer digit value.
ques = center.tostring (); // convert the decimal bits into a string to find the length of the decimal.
While (central.Length <Cent) {// Make up the number to the specified digits.
center = "0" + center;
}
if (iStHousand == 0) // No need thousands of quota.
Return ((sign)? '': '-') + num + '.' + CentS);
// Format the integer part for thousands of points.
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;
} ());