


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

AI Hentai Generator
Generate AI Hentai for free.

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

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig
