[JS] Lesson Seven: Value

1.number reference type

1.1 Number as a constructor

var num=new Number(10);

1.2 Number function as a tool

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

[operation result]
Here Insert Picture Description

1.3 Number Properties on constructor

console.log(Number.POSITIVE_INFINITY);//正无穷
console.log(Number.NEGATIVE_INFINITY);//负无穷
console.log(Number.NaN);//不是一个数
console.log(Number.MAX_VALUE);//JS中最大数
console.log(Number.MIN_VALUE);//JS中最小数
console.log(Number.MAX_SAFE_INTEGER);//JS中能够精确表示的最大整数
console.log(Number.MIN_SAFE_INTEGER);//JS中能够精确表示的最小整数

[operation result]
Here Insert Picture Description

1.4 Number constructor methods

  1. Number.isFinite (number) to detect if a number is the number of poor has
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) detecting 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) to detect if a number is an integer safe
  2. Number.isNaN (number) detecting 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
  1. Number.parseInt (String, the radix)
    String: string conversion
    radix: hexadecimal Type
    Return Value: integer number after conversion
    with the global parseInt (string, radix) equivalent 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)
    with the global parseFloat (string) method is equivalent
console.log(Number.parseFloat('0.1'));  //0.1
console.log(Number.parseFloat('12'));  //12
console.log(Number.parseFloat('12.3abc'));  //12.3

1.5 Number methods prototype chain

  1. number.prototype.toFixed (fractionDigits)
    Function: the value of the specified number of decimal places to retain
    franctionDigits: the number of decimal digits
    Return Value: fixed-point format string
var num=10.005;
num.toFixed(2); //10.01
100.01.toFixed(2);  //100.01
(100).toFixed(2);   //100.00 整数要加括号
  1. number.prototype.toExponential (franctionDigits)
    Function: Convert the value exponentially, it returns a string
    franctionDigits: the number of decimal digits
    Return Value: the exponent string
(1).toExponential(2);    //1.00e+0 
(10000).toExponential(1);      //1.0e+4
  1. number.prototype.toPrecision (precision)
    Function: the number of bits specified in the form of realization of the value returned string
    precision: bits of precision
    Return Value: Specifies the number of bits represented by a string
(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
  1. number.prototype.toString ([radix])
    Function: converts a string value
    radix: into several hex, decimal default value
    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

Property 1.Math object

Here Insert Picture Description

Methods 2.Math object

  1. Math.abs (x)
    returns the numeric type
Math.abs(-1);   //1
  1. Math.round (x)
    function: the number x rounded up, and returns 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: rounded 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 parameter
Published 51 original articles · won praise 5 · Views 4169

Guess you like

Origin blog.csdn.net/qq_43519498/article/details/103890055