Data type-number

Source: Internet
Author: User

Number has integers, floating-point values , and Nan 's points.

integer Basic numeric literal format is decimal, and there are eight binary and hexadecimal

Decimal can be entered directly:

var intnum=; // integer

The octal first bit must be 0, then the octal number sequence (0~7), if the value of the literal value is out of range, then the leading 0 is ignored and the subsequent value is parsed as a decimal value. Here's an example:

var octalnum1=070; // eight-in-a-year var octalnum2=079; // The value 9 is invalid in octal, so it is parsed as var octalnum3=; // invalid octal, will be resolved to 8

Note: Octal literals are not valid under strict mode and will throw an error!

The first two digits of the hexadecimal literal must be 0x, followed by any hexadecimal digits (0~9 and a~f), and the letters can be uppercase or lowercase. Here's an example:

var hexnum1=0xA; // 16 binary var hexnum2=0x1f; // 16 binary

At the time of calculation, all octal and hexadecimal values are eventually converted to decimal values.

A floating-point value is the number that must contain a decimal point and must be followed by only one digit. There is no number before the decimal point, but it is not recommended. For example:

var floatnum1=1.1; var floatnum2=0.1; var floatnum3=. 1; // Not recommended

Floating-point values can be represented by E (i.e. scientific notation) for large or small values. The value represented by the E notation equals the number in front of e multiplied by the exponential power of 10. For example:

var floatnum1=3.125e7; // equals 31250000 var floatnum2=3.125e-7; // equals -31250000

The highest precision of a floating-point value is 17 decimal places, but there will be errors in the calculation of a particular combination, such as:

0.1+0.2=0.30000000000000004; // not 0.3 .

Due to memory limitations, ECMAScript cannot save all the values in the world. The minimum value that ECMAScript can represent is saved in Number.min_value, which is 5e-324 and can represent the maximum value saved in Number.MAX_VALUE, which is 1.7976931348623157e+308. If the result of a calculation results in a value that exceeds the range of JavaScript values, the value is automatically converted to a special infinity value. If the value is negative, it will be converted to-infinity (negative infinity), and if the value is positive, it will be converted to Infinity (positive infinity).
The function isfinite () can detect if the range is exceeded, for example:

var result=number.max_value+Number.min_value;alert (isfinite (Result)); // true

NaN, a non-numeric value (not a number) is a special value that represents a case where an operand that would have returned a numeric value does not return a value (so that no error is thrown). For example, in other programming languages, dividing any number by 0 will result in an error, thus stopping code execution. However, in ECMAScript, any number divided by 0 will return Nan, so no other code execution will be affected.
The Nan itself has two unusual features.

First, any operation involving Nan (example: NAN/10) will return Nan, a feature that can cause problems in a multi-step calculation.

Second, Nan is not equal to any value, including the Nan itself. For example:

alert (Nan==nan); // Fales

For these two features, ECMAScript defines the isNaN () function. This function takes a parameter, which can be any type, and the function will help us determine whether the parameter is "not a value". IsNaN () after receiving a value, it attempts to convert the value to a number. Some values that are not numeric are converted directly to numeric values, such as a character channeling "10" or a Boolean value. Any value that cannot be converted to a number will cause the function to return true. For example:

Alert (IsNaN (NaN)); // true alert (IsNaN (ten)); // False (10 is a numeric value) alert (IsNaN ("ten")); // False (can be converted to a value of ten) alert (IsNaN ("Blue)");//true alert (IsNaN (true)); // False (True can be converted to a value of 1)


There are three functions to convert non-numeric values to numeric values: Number (), Pareseint (), and Paresefloat ().

Number () can be used for any data type.

parseint () and parsefloat () are specifically used to convert character channeling into numerical values.

The conversion rules for the number () function are as follows:

• If it is a Boolean value, True and false are converted to 1 and 0, respectively.

• If it is a numeric value, simply wear any return.

• If it is a null value, returns 0.

• If it is undefined, return nan.

• If it is a string, the following rules apply:

1. If the string contains only numbers (including cases preceded by a plus or minus sign), it is converted to a decimal value, that is, "1" becomes 1, "123" becomes 123, and "011" becomes 11;

2. If the string contains a valid floating-point format, such as "1.1", it is converted to the corresponding floating-point value (also, the preamble is ignored);

3. If the string contains a valid hexadecimal format, such as "0xf", it is converted to a decimal integer value of the same size;

4. If the string is empty (does not contain any numbers), convert it to 0;

• If it is an object, call the ToString () method of the object and then convert the return amount to the previous rule. If the result of the conversion is Nan, the object's ToString () method is then converted again to the string value returned in accordance with the rule of the facet.

5. If the character channeling contains characters other than the above format, convert it to Nan.

See the following example according to the rules:

  var  num1=number ("  hello word   "); // nan   var  num2=number (); // 0   var  num3=number ( " 0000011  " ); // 11   var  num4=number ( " ture  " ); // 1  

parseint () It ignores whitespace in front of the string until the first non-whitespace character is found. If the first character is not a numeric character or a minus sign, Nan is returned.

parseint () can also recognize various integer formats (decimal, octal, and hexadecimal). Because of this knowledge, we should specify the number of binary parameters for the specified second parameter. For example:

      varNum1=parseint ("AF", -);//Parse by hex, 175      varNum2=parseint ("Ten",2);//by binary parsing, 2      varNum3=parseint ("Ten",8);//by octal parsing, 8      varNum4=parseint ("Ten",Ten);//by decimal resolution, ten      varNum5=parseint ("Ten", -);//by hexadecimal parsing,

Parsefloat () is similar to the parseint () function and parses each character starting with the first character (position 0). It is also parsed until the end of the string, or until an invalid floating-point numeric character is encountered. That is, the first decimal point in the string is valid, and the second decimal point is invalid, so the string following it is ignored. For example:

Alert (parsefloat ("22.23.23")); // 22.23

Unlike parseint (), it parses only the decimal value, so it does not specify the usage of the base number with the second argument.
Note: If the string contains a number that can be resolved to an integer (no decimal point, or the decimal point is 0), it returns an integer.

Here are a few typical examples of using parsefloat () to convert numeric values:

varNum1=parsefloat ("1234blue");//1234varNum2=parsefloat ("0xA");//0varNum3=parsefloat ("22.5");//22.5varNum4=parsefloat ("22.34.5");//22.34varNum5=parsefloat ("0908.5");//908.5varNum6=parsefloat ("3.125e7");//31250000

Data type-number

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.