


js about precise calculation and numerical formatting and direct reference to js files_javascript skills
(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.length
}
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;
}());

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.
