Home > Web Front-end > JS Tutorial > js about precise calculation and numerical formatting and direct reference to js files_javascript skills

js about precise calculation and numerical formatting and direct reference to js files_javascript skills

WBOY
Release: 2016-05-16 17:01:59
Original
957 people have browsed it
Copy code The code is as follows:

(function () {
var calc = {
/*
Function, addition function, used to get accurate addition results
Note: The addition result of JavaScript will have errors , it will be more obvious when adding two floating point numbers. This function returns a more accurate addition result.
Parameters: arg1: the first addend; arg2 the second addend; d the number of decimal places to be retained. (This parameter does not need to be passed, otherwise the number of decimal places will not be 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 accurate subtraction results
Description: The function returns a more accurate subtraction result.
Parameters: arg1: the first addend; arg2 the second addend; d. The number of decimal places to be retained (you can not pass this parameter, if not, the number of decimal places will not be processed
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 obtain accurate multiplication results
Description: The function returns more accurate multiplication results.
Parameters: arg1: the first multiplier; arg2 the second multiplier; the number of decimal places to be retained in d (you can not pass this parameter, if not, the number of decimal places will not be processed)
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(10, m);
return typeof d !== "number" ? Number(resultVal) : Number(resultVal.toFixed(parseInt (d)));
},
/*
Function: division function, used to obtain accurate division results
Description: The function returns a more accurate division result.
Parameters: arg1: divisor; arg2 dividend; the number of decimal places to be retained in d (you can not pass this parameter, if not, the number of decimal places will not be processed)
Call: Calc.Div(arg1,arg2)
Return value: the result of dividing arg1 by 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 decimal place to be retained (Number)
@param isThousand Whether thousandths is required 0: Not required, 1: Required (numeric 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 that the incoming value is a numeric type.
num = "0";
if(isNaN(cent))//Ensure that the incoming decimal place is a numeric value.
cent = 0;
cent = parseInt(cent);
cent = Math.abs(cent);//Find the number of decimal places and make sure it is a positive integer.
if(isNaN(isThousand) )//Make sure whether the thousandth place passed in is a numeric type.
isThousand = 0;
isThousand = parseInt(isThousand);
if(isThousand < 0)
isThousand = 0;
if(isThousand >=1) //Make sure the value passed in is only 0 or 1
isThousand = 1;
sign = (num == (num = Math.abs(num))); //Get the sign (positive/negative number)
//Math.floor: Return the largest integer less than or equal to its numerical parameter
num = Math.floor(num*Math.pow(10,cent) 0.50000000001);/ /Convert the specified decimal places to integers first. Round off the extra decimal places.
cents = num%Math.pow(10,cent); //Find the decimal place value.
num = Math.floor( num/Math.pow(10,cent)).toString();//Find the value of the integer digits.
cents = cents.toString();//Convert the decimal digits into a string to find the length of the decimal digits .
while(cents.lengthcents = "0" cents;
}
if(isThousand == 0) // No thousandth place character is required.
return (((sign)?'':'-') num '.' cents);
//Format the integer part in the thousandth place.
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;
}());
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template