# [JS] Lesson 7: Numbers

## 1.number reference type

#### 1.1 Number as constructor

```var num=new Number(10);
```

#### 1.2 Number as a tool function

```console.log(Number('1.1'));
console.log(Number(0xBC));
console.log(Number(0b1011));
console.log(Number(true));
console.log(Number('1.a6'));
```

[Run result]

#### Properties on 1.3 Number constructor

```console.log(Number.POSITIVE_INFINITY);//plus infinity
console.log(Number.NEGATIVE_INFINITY);//negative infinity
console.log(Number.NaN);//Not a number
console.log(Number.MAX_VALUE);//Maximum number in JS
console.log(Number.MIN_VALUE);//Minimum in JS
console.log(Number.MAX_SAFE_INTEGER);//The largest integer that can be accurately represented in JS
console.log(Number.MIN_SAFE_INTEGER);//The smallest integer that can be accurately represented in JS
```

[Run result]

#### Method on 1.4 Number constructor

1. Number.isFinite(number) detects whether a number is infinite
```console.log(Number.isFinite(0));  //true
console.log(Number.isFinite(Number.POSITIVE_INFINITY));  //false
console.log(Number.isFinite(Number.NaN));  //false
```
1. Number.isInteger(number) detects whether a number is an integer
```console.log(Number.isInteger(0.1));  //false
console.log(Number.isInteger(Infinity));  //false
console.log(Number.isInteger('1'));  //false
console.log(Number.isInteger(1));  //true
console.log(Number.isInteger(1.0));  //true
```
1. Number.isSafeInteger(number) detects whether a number is a safe integer
2. Number.isNaN(number) detects whether a number is an integer
```console.log(Number.isNaN(0.1));  //false
console.log(Number.isNaN(NaN));  //true
console.log(Number.isNaN(0/0));  //true
console.log(Number.isNaN('string'));  //false
console.log(Number.isNaN(1/'abc'));  //true
```
String:Converted string
Return value: converted integer
Equivalent to the global parseInt(string, radix) method
```console.log(parseInt("1")); //1
console.log(parseInt("1.1")); //1
console.log(parseInt("1a2.23")); //1
console.log(parseInt("abc")); //NaN
```
1. Number.parseFloat(string)
Equivalent to the global parseFloat(string) method
```console.log(Number.parseFloat('0.1'));  //0.1
console.log(Number.parseFloat('12'));  //12
console.log(Number.parseFloat('12.3abc'));  //12.3

```

#### Method on 1.5 Number prototype chain

1. number.prototype.toFixed(fractionDigits)
Function: Keep the number of decimal places in the specified number of digits
FractiononDigits: Number of digits after the decimal point
Return value: fixed-point formatted string
```var num=10.005;
num.toFixed(2); //10.01
100.01.toFixed(2);  //100.01
(100).toFixed(2);   //100.00 Integer bracketed
```
1. number.prototype.toExponential(franctionDigits)
Function: Converts a numeric value to an exponential form and returns a string
FractiononDigits: Number of digits after the decimal point
Return value: String in exponential form
```(1).toExponential(2);    //1.00e+0
(10000).toExponential(1);      //1.0e+4
```
1. number.prototype.toPrecision(precision)
Function: Returns a string representation of the value in the specified number of digits
Precision: number of precision digits
Return value: The string represented by the specified number of bits
```(1.123).toPrecision();        //1.123
(1.123).toPrecision(2);       //1.1
(1.123).toPrecision(5);       //1.1230
(1.523).toPrecision(1);        //2
(1234.5).toPrecision(2);      //1.2e+3
```
Function: Convert numeric values to strings
radix: converted to decimal, default is 10
Return value: String
```console.log((10).toString());     //10
console.log((10).toString(2));    //1010
console.log((10).toString(8));    //12
console.log((10).toString(10));    //10
console.log((10).toString(16));     //a
```

## 2.Math Objects

#### 2. Methods of Math objects

1. Math.abs(x)
Return Number Type
```Math.abs(-1);   //1
```
1. Math.round(x)
Function: Round the number x and return a number
```Math.round(1.5);    //2
Math.round(1.4);   //1
```
1. Math.ceil(x)
Function: Rounding up
```Math.ceil(1.1);  //2
```
1. Math.floor(x)
Function: Round down
```Math.ceil(1.9);  //1
```
1. Math.max(...)
2. Math.min(...)
3. Math.random()
Function: Returns a random number in the range [0,1]
No parameters
4. ...
Published 51 original articles. Praise 5. Visits 4128

Added by onepixel on Sat, 11 Jan 2020 05:44:34 +0200