JS about accurate calculation and numerical formatting as well as direct indexing JS file _javascript skills

Source: Internet
Author: User
Tags abs mul pow
Copy Code code as follows:

(function () {
var calc = {
/*
function, addition function, used to get the exact addition result
Description: JavaScript addition results will be error, in two floating-point number added when it is more obvious. This function returns a more precise addition result.
Parameters: Arg1: The first addends; arg2 the number of decimal places to keep (you can not pass this parameter, if not the number of decimal places)
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 (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 get exact subtraction results
Description: The function returns a more accurate subtraction result.
Parameters: Arg1: The first addends; arg2 the number of decimal places to keep (this parameter can not be passed and the number of decimal places is not processed if not passed)
Call: Calc.sub (ARG1,ARG2)
Return value: The result of subtracting two numbers
*/
Sub:function (Arg1, arg2) {
Return Calc.add (Arg1,-number (ARG2), arguments[2]);
},
/*
Function: multiplication function, used to get accurate result of multiplication
Description: The function returns more accurate multiplication results.
Parameters: Arg1: First multiplier; arg2 second multiplier; d to keep the number of decimal places (you can not pass this parameter, if not the number of decimal digits)
Call: Calc.mul (ARG1,ARG2)
Return value: The 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 (M);
Return typeof D!== "number"? Number (Resultval): Number (resultval.tofixed (parseint (d)));
},
/*
Function: Division function, used to get exact division results
Description: The function returns a more precise division result.
Parameters: arg1: divisor; arg2 divisor; d The number of decimal places to keep (you can not pass this argument, if not the number of decimal places)
Call: Calc.div (ARG1,ARG2)
Return value: Arg1 In addition to ARG2 results
*/
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 (M);
Return typeof D!== "number"? Number (Resultval): Number (resultval.tofixed (parseint (d)));
},
/*
The values are rounded and formatted.
@param num Value (number or string)
@param number of decimal places to keep cent
@param whether the Isthousand need to be 0: not required, 1: required (numerical type);
A string @return format, 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 that the incoming value is a numeric type.
num = "0";
if (isNaN (cent))//ensures that incoming decimal places are numeric values.
cent = 0;
cent = parseint (cent);
cent = math.abs (cent);//Find the number of decimal places to ensure a positive integer.
if (isNaN (Isthousand))//ensures that the thousand-bit value type is required for incoming.
Isthousand = 0;
Isthousand = parseint (Isthousand);
if (Isthousand < 0)
Isthousand = 0;
if (Isthousand >=1)//Ensure that the number passed in is only 0 or 1
Isthousand = 1;
Sign = (num = (num = math.abs (num));/get symbol (positive/negative)
Math.floor: Returns the largest integer less than or equal to its numeric argument
num = Math.floor (Num*math.pow (10,cent) +0.50000000001);//converts the specified decimal places to integers. Extra decimal places rounded.
cents = Num%math.pow (10,cent); Find the decimal digit value.
num = Math.floor (Num/math.pow (10,cent)). toString ();//Find an integer digit value.
cents = cents.tostring ();//converts decimal places to strings to find decimal lengths.
while (cents.length<cent) {//complements the decimal places to the specified number of digits.
cents = "0" + cents;
}
if (Isthousand = = 0)//no thousand characters are required.
Return (((sign)? ': '-') + num + '. ' + cents);
The integer portion is formatted as a decimal point.
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;
}());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.