Javascript-number type value

Source: Internet
Author: User
Tags php website string to number
This article mainly introduces the knowledge of the basic JavaScript type value-Number type, which has good reference value. Let's take a look at it below. Overview

In JavaScript, numbers are represented in the IEEE754 format. Therefore, integers and floating-point numbers are stored in 64-bit floating-point numbers. That is to say, there is no Decimals in JavaScript. However, some operations must be completed by integers. Therefore, JavaScript sometimes converts 64-bit floating point numbers to 32-bit integers before performing operations.

Integer

JavaScript provides four Representation Methods for integers:

1. Binary: A value with a prefix of 0 B. An error is returned if a number other than 0 or 1 is displayed.

2. octal: A value with a prefix of 0 o, or a number (0-7) after 0 ). If the value range is exceeded, the first digit 0 is ignored, which is regarded as a decimal number.

Note: The octal literal is invalid in strict mode, leading to an error thrown by the JavaScript engine that supports this mode.

3. hexadecimal: a prefix 0x followed by any hexadecimal number (0 ~ 9 and ~ F). All uppercase and lowercase letters are allowed. If it is out of the range, an error is returned.

4. Decimal


Var num2 = 0b11; console. log (num2); // 3 var num2 = 0b12; console. log (num2); // error var num8 = 0o76; console. log (num8); // 02 var num8 = 0o78; console. log (num8); // error var num16 = 0x2a; console. log (num16); // 42 var num16 = 0x2 h; console. log (num16) // Error


Floating Point Number

A floating point number must contain at least one decimal point. Unlike integers, floating-point numbers can only be expressed in decimal format.

The precision of floating-point numbers is far less than that of integers. Therefore, you must be careful when designing floating-point numbers.

For example:


 console.log(0.1 + 0.2 == 0.3); //false 0.30000000000000004 console.log(0.6/0.2); //2.9999999999999996


Scientific notation

For those extremely large and extremely small values, they can be represented by floating point values in e notation (Scientific Notation. The value in e notation is equal to the value before e multiplied by the exponential power of 10.

In the following two cases, JavaScript will automatically convert the value into a scientific notation, and in other cases, it is represented directly in the literal form.

1. the number before the decimal point is 21 more digits.


 console.log(1234567890123456789012);// 1.2345678901234568e+21 console.log(123456789012365648787); //123456789012365660000 


2. The number of digits after the decimal point is greater than 5.


 console.log(0.0000006);//6e-7 console.log(0.000006); //0.000006


Value Range

Due to memory limitations, ECMAScript cannot store all the values in the world, so it has the maximum and minimum values.

The minimum value is stored in Number. MIN_VALUE. The value is 5e-324.

The maximum value is stored in Number. MAX_VALUE. The value is 1.7976931348623157e + 308.


 console.log(Number.MIN_VALUE) //5e-324 console.log(Number.MAX_VALUE); //1.7976931348623157e+308


If the number exceeds the maximum value, javascript returns Infinity, which is called forward overflow. If it is equal to or greater than the minimum negative value-1023 (that is, very close to 0 ), javascript directly converts this number to 0, which is called underflow)

If you want to determine whether a numeric value is poor, you can use the isFinite () function. This function returns true if the parameter is between the minimum and maximum values.


 var result = Number.MAX_VALUE + Number.MAX_VALUE; console.log(isFinite(result)); //false


Special Value

1. + 0 and-0

The two zeros are equivalent in the case of a large logarithm.


 -0 === +0; //true 0 === -0; //true 0 === +0; //true


But it is different when it is used as the denominator.

1/-0 == 1/+0; //false

2. infinity

Infinity indicates "infinite", which is used to represent two scenarios. One is that a positive value is too large, or a negative value is too small to be expressed. The other is a non-0 value divided by 0 to obtain Infinity.


 Math.pow(2,Math.pow(2,100));//Infinity 1/0;//Infinity


Infinity can only be involved in the calculation result itself, 0, or NaN


* Infinity;//Infinity- Infinity;//-Infinity+ Infinity;//Infinity/ Infinity;//0 Infinity / 2;//Infinity Infinity * Infinity;//Infinity Infinity - Infinity;//NaN Infinity + Infinity;//Infinity Infinity / Infinity;//NaN


3. NaN

This value indicates a condition in which the operand of the value to be returned has not been returned.

NaN is not equal to any value, including itself, and any operation involving NaN will return NaN


 5 - 'x'; //NaN Math.acos(2); //NaN 0 / 0; //NaN NaN == NaN;//false NaN == Infinity;//false


NaN is not an independent data type, but a special value. Its data type still belongs to Number.

typeof NaN; //number

The isNaN method can be used to determine whether a value is NaN. However, isNaN is only valid for the value. If other values are input, they are first converted to numerical values. For example, when a string is input, the string is first converted to NaN, so the return value is true. This deserves special attention. That is to say, if isNaN is a true value, it may not be NaN, but a string.


IsNaN ('hello') // true // equivalent to isNaN (Number ('hello') // true


A more reliable way to judge NaN is to use NaN as the only value in javascript that is not equal to itself.


 function isNaN(value){ return value != value; }


Number Conversion

There are three functions that can convert non-numeric values into numeric values: Number (), parseInt (), and parseFloat (). Here, Number () can convert any type of value to a numerical value, while parseInt () and parseFloat () are only applied to the conversion from string to Number.

Number ()

Conversion rules:

1. If it is a Boolean value, true and false will be converted to 1 and 0 respectively.

2. If the value is null, 0 is returned.

3. If it is undefined, NaN is returned.

4. If it is a string, follow the following rules:

(1) If the string contains only digits in decimal or hexadecimal notation, convert it to a decimal number.

Note: Number () is a string that does not recognize Octal numbers and is processed in decimal format.

The string '1' does not return an error, but the number 1. 2 will return an error.

(2) If the string is a null string or a space string, it is converted to 0.

(3) In other cases, the string is converted to NaN


Number(true) //1 Number(null) //0 Number(undefined) //NaN Number("0123") //123 Number("0x123") //291 Number("0.2") //0.2 Number("") //0 Number("asd") //NaN


ParseInt ()

ParseInt () is used to convert a string to an integer. When converting a string, the space before the string is ignored until the first non-space character is found. If the first character is not a numeric character or a negative number, parseInt () returns NaN. If yes, the parsing continues until the parsing is complete or non-numeric characters are encountered.


 console.log(parseInt(' 123.8px'));//123 console.log(parseInt(' 123.8 '));//123 console.log(parseInt(' -123.8px'));//-123 console.log(parseInt('a123.8px'));//NaN console.log(parseInt('0 123.8px'));//0


Note: In ECMAScript5, parseInt () does not have the ability to parse gossip. For example, if the octal value is "070", the previous "0" will be ignored and the decimal value of 70 will be obtained.

To eliminate the above confusions that may occur when you use the parseInt () function, you can provide this function with the second parameter: base number used for conversion (in hexadecimal notation)


 parseInt("070") //70 parseInt("070",8) //56


ParseFloat ()

ParseFloat () is specially used for String Conversion of floating point numbers. Similarly, the space in front of the string is ignored during parsing until the first non-space character is found and then parsed until the end of the string or an invalid floating point number character is found.


console.log(parseFloat(' 0123.px'));//123 console.log(parseFloat(' 123.px'));//123 console.log(parseFloat(' 123.1px'));//123.1 console.log(parseFloat(' 123.1.2px '));//123.1 console.log(parseFloat(' -123.0px'));//-123 console.log(parseFloat('.123.1px'));//0.123 console.log(parseFloat('0 123px'));//0


Note: parseFloat () is only parsed in decimal format. Therefore, the hexadecimal string is always converted to 0. Therefore, there is no second parameter to specify the base number.

parseFloat("0xA") //0

Note: the result of Number ('') is 0, and the result of parseInt ('') and parseFloat ('') is NaN.

For more articles about the Number type value, refer to the Chinese PHP website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.