Introduction to the
   Numbers, also known as numbers or numbers, are one of the fundamental data types of JavaScript. When a number appears directly in a program, it is called a numerical direct quantity. In JavaScript programs, any number entered directly is treated as a numeric direct quantity. And in JavaScript, all numbers are stored as 64-bit floating-point numbers, including integers. For example, 2 is the same number as 2.0.
type
  javascript does not distinguish between integer and floating point numbers, and uses Number to represent the Number type. Common Number types are as follows:
Typeof (96) // Integer Typeof (3.1415926) // Floating point typeof(1.23E456) // Scientific notation Typeof (0x123) // Hexadecimal Typeof (-1) // negative typeof(NaN) // Non-numeric abnormal number representation or cannot calculate the result typeof(Infinity) // positive Infinity corresponds to -Infinity is negative InfinityCopy the code
Judgment method
Nbsp  In JavaScript, we want to determine the type of a number in the following ways:
1.typeof && isNaN && isFinite
&nbSP&NBsp In general, the number in our words refers to the number that can be calculated, so we need to exclude NaN and Infinity, two special number types. There is a function that filters NaN and Infinity. use isFinite to determine if a number isFinite. The isFinite method checks the value of its parameters. Returns false if the argument is NaN, plus or minus infinity, and true otherwise. Use isNaN if you only need to distinguish nans
2.Object.prototype.toString.call
Object. The prototype. ToString. Call is also a common method of data types, at the time of judgment digital can use it to replace typeof:
function isNumber(target) {
return Object.prototype.toString.call(target) === '[object Number]' && isFinite(target)
}
Copy the code
3. +
Add a + to the front of the object, which converts to number, which equals itself to indicate that the object is a number. Note that Infinity returns true as follows:
function isNumber(target) {
return target === +target
}
Copy the code
4. The regular
To judge by regularity, scientific notation and other base representations are not considered, and the character ‘123’ is considered a number without the numeric character.
function isNumber(obj) { var reg = /^[0-9]+.? [0-9]*$/ return reg.test(obj) }Copy the code
The digital method
1.Number()
Number() can be used to convert JavaScript variables to numeric values, and NaN if a variable cannot be converted:
x = true; Number(x); // return 1 x = false; Number(x); // return 0 x = new Date(); Number(x); 1404568027739 x = "10" Number(x); // return 10 x = "10 20" Number(x); / / returns NaNCopy the code
2. The parseInt (), parseFloat ()
ParseFloat () and parseInt() parse a string and return a value. Spaces are allowed. Only the first number is returned, and a variable that cannot be converted is converted to NaN:
parseInt("10"); // Returns 10 parseFloat("10"); // return 10 parseInt("10.33"); // Returns 10 parseFloat("10.33"); // return 10.33 parseInt("10 20 30"); // Return 10 parseFloat("10 20 30"); // return 10 parseInt("10 years"); // Return 10 parseFloat("10 years"); // return 10 parseInt("years 10"); // Return NaN parseFloat("years 10"); / / returns NaNCopy the code
3.toFixed()
ToFixed () returns a string value that contains a number specifying a decimal number, and toFixed(2) is perfect for handling money:
Var x = 9.656; x.toFixed(0); // return 10 x.tofixed (2); // return 9.66x.tofixed (4); // Return 9.6560x.tofixed (6); / / return 9.656000Copy the code
4.Math
The Math object is used to perform Math tasks. You don’t need to create it, you can invoke all of its properties and methods by using Math as an object. Some common Math methods are as follows:
Math. Abs (x) -- -- -- -- -- -- -- -- the absolute value math.h round (x) -- -- -- -- -- -- -- -- rounded math.h ceil (x) -- -- -- -- -- -- -- -- up integer Math. The floor (x) -- -- -- -- -- -- -- -- down to integer Math. Max ([val1, val2 [[,...]]]] -- -- -- -- -- -- -- take the maximum math.h min ([val1, val2 [[,...]]]] -- -- -- -- -- -- -- take the smallest math.h random -- -- -- -- -- -- -- -- random Numbers (between 0 and 1) Math. Cos (x) -- -- -- -- -- -- -- -- cosine Math. J exp (x) -- -- -- -- -- -- -- -- the ex math.h log (x) -- -- -- -- -- -- -- -- ey return the value of y = x math.h SQRT (x) -- -- -- -- -- -- -- -- square root math.h pow (x, y) -- -- -- -- -- -- -- -- x y power math.h trunc (x) -- -- -- -- -- -- -- -- remove the decimal part of a number, and returns the integer part. Math. Sign (x) -- -- -- -- -- -- -- -- to judge whether a number is positive, negative, or zero math.h CBRT (x) -- -- -- -- -- -- -- to calculate the cube root of a numberCopy the code
5.toString()
ToString () returns a number as a string, and all numeric methods can be used for any type of number (literal, variable, or expression) :
var x = 123; x.toString(); // Return 123 (123).tostring () from x; // From the text 123 returns 123 (100 + 23).tostring (); Return 123 from the expression 100 + 23Copy the code
6.toExponential()
Returns the numeric string representation in exponential notation that takes a parameter specifying the number of digits after the decimal point.
Let a = 99.6633; Let a1 = 'string:' + a.too (); // "echo +1 +1" let a2 = '// echo +1 +1'; // "string: 9.97e+1"Copy the code
7.toPrecision()
Returns a string representation of the numeric object with the specified precision, taking an integer that specifies the number of valid numbers.
Let a = 1.2345; Let a1 = 'string:' + a.torecision (); // "string: 1.2345" let a2 = 'string:' + a.torecision (1); // "string: 1" let a2 = 'string:' + a.torecision (2); // "string: 1.2"Copy the code
8.Number.XXX
(1), number.max_value
What it does: Represents the largest value that can be represented in JavaScript. Grammar: Number. MAX_VALUE; Return value: return the maximum number that js can represent, close to $1.79E+308;
(2), number.min_value
Function: Represents the smallest value that can be represented in JavaScript. Grammar: Number. MIN_VALUE; Return value: Returns the largest number that js can represent, close to 5E-324;
(3), Number. IsInteger
Function: Used to determine whether a given parameter is an integer; Grammar: Number. IsInteger (Number); Return value: Returns Boolean;
The numerical computation
The operator
1, define,
Operators in JavaScript are mainly used to concatenate simple expressions to form complex expressions
2. Addition: x + y
The addition operator decides at run time whether to add or join. In other words, different operators lead to different syntactic behaviors, a phenomenon called “overload”.
(1) Add when both operands are numbers
console.log(2+4); / / 6
(2) If both parameters are strings, or if one parameter is a string, the other parameter is converted to a string for string concatenation
‘3’ + 4 + 5 // “3 “+ 4 + 5 // “75”
(3) in the case that the parameter has an object
In general, the valueOf method of an object always returns the object itself, or it can be customized. 3.2 The toString method of an object is then automatically called to convert it to a string (if valueOf directly returns a primitive value, The toString method of the toString object returns [object object] by default.
var obj = { p: 1 };
obj + 2 // "[object Object]2"
Copy the code
3.3 Customize the valueOf method or toString method to get the desired result
var obj = { valueOf: function () { return 1; }}; obj + 2 // 3Copy the code
3. Other arithmetic operators
For other operators, the number is cast before the operation, and then the operation. The object calls valueOf or toString, and if it cannot be converted, outputs NaN
Traction operator: x * y Multiplication operator (Division) : X/Y Remainder: X % Y Increment operator (x/Y) : int (x/y) : int (x/y) : int (x/y) : int (x/y) : int (x/y)) ++ X or X ++ Decrement operators: -- X or X -- Negate operators: -x Index operator: X **y Convert to number: + XCopy the code
4. The assignment operator
The assignment operator is used to assign values to variables. The most common assignment operator, of course, is the equal sign. The expression x=y means to assign y to x. In addition, JavaScript provides 11 other assignment operators. Var z=y=x;
X += y // is the same as x = x + y x -= y // is the same as x = x-y x *= y // is the same as x = x * y x /= y // is the same as x = x % y // is the same as x = x % y X > > = y / / is equivalent to x = x > > y x < < = y / / equal to x = x < < x > y > > = y / / is equivalent to x = x > > > x & y = y / / is equivalent to x = x & y | x = y / / equal to x = x | x ^ = y/y/x = x ^ y equivalentCopy the code
5. Bit operators
The bit operator only works on integers. If an operator is not an integer, it is automatically converted to an integer and then executed
1, or operation (OR) :
Symbol for |, said one of the two binary is 1, the result is 1, otherwise 0. 0000 0010 | 0000 0001 0000 0011 (3)
2, and operation (and) :
The symbol & means that if both bits are 1, the result is 1, otherwise 0. 0000 0000 0000 (0)
3, No operation (not) :
The symbol ~ is used to change a binary bit to its opposite. ~ 0000 0010 is equal to 1111 1101
Xor operation (XOR) :
The symbol is 1, which indicates that the result is 1 if only one of two binary bits is 1, and 0 otherwise.
5. Left shift operation:
Symbol for < <
6. Right shift operation:
Symbol for > >
7. Zero filled right Shift
Symbol for > > >
Floating point operation
In our daily life, using arithmetic operators, numerical values can participate in various calculations, such as addition, subtraction, multiplication, division and other operations. However, because JavaScript uses the IEEE754 format to represent integers and floating-point values, there are often problems with IEEE754 standards for 64-bit double-precision floating-point numbers. Floating-point numbers are infinite when expressed in binary, truncated binary numbers are converted to decimal, for example, 0.1+0.2 becomes 0.30000000000000004, which requires some special methods to solve this problem:
1. Add
Number.prototype.add = function(arg){
var r1,r2,m;
try{r1=this.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2))
return (this*m+arg*m)/m
}
Copy the code
2. The subtraction
Number.prototype.sub = function(arg){
var r1,r2,m;
try{r1=this.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2))
return (this*m-arg*m)/m
}
Copy the code
3. The multiplication
Number.prototype.mul = function (arg) {
var m=0,s1=this.toString(),s2=arg.toString();
try{m+=s1.split(".")[1].length}catch(e){}
try{m+=s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}
Copy the code
4. Division
Number.prototype.div = function (arg){
var t1=0,t2=0,r1,r2;
try{t1=this.toString().split(".")[1].length}catch(e){}
try{t2=arg.toString().split(".")[1].length}catch(e){}
with(Math){
r1=Number(this.toString().replace(".",""))
r2=Number(arg.toString().replace(".",""))
return (r1/r2)*pow(10,t2-t1);
}
}
Copy the code