"JavaScript Advanced Programming" Learning summary three (2)

Source: Internet
Author: User
Tags numeric value

Introduction: A New day, insist on learning, summary. Yesterday's article looked again today, found that the updated knowledge point is really a little less, today to write more than a line. Then this blog post continues to summarize the knowledge points of data concepts and basics.

Number type:

The number type should be the most interesting type in JavaScript. This type uses the IEEE754 format to represent integers and floating-point values (double-precision floating-point numbers), and ECMA-262 defines different numeric literal formats to support various numeric types. Like what:

Decimal: var intnum=65; (Integral type)

var octalnum=070 (octal 56), octal first digit must be 0, then octal number sequence (0~7), octal literal is invalid in strict mode, cause the supported JavaScript engine throws an error.

var hexnum=0xa (16 binary 10), hexadecimal literal the first two digits must be 0x, followed by any hexadecimal digits (0~9 and a~f), where a~f can be uppercase or lowercase.

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

Floating point value:

 A floating-point number means that a decimal point must be included in the value, and that there must be at least one digit after it.

Since the memory space required to hold the floating-point value is twice times that of the integer value, ECMAScript will lose no chance to convert the floating-point number to an integer, so it is clear that if there is no number after the decimal point, then it can be saved as an integral type.

For example: Var floatnum=1.0; This will be parsed into 1.

For those large or small values, you can use the E notation (scientific notation), the e notation means that the value is equal to the value of the front of e multiplied by the exponential power of 10, for example: Var floatnum=3.14e2 then this number is 314.

The highest precision of a floating-point number is 17 decimal places, but the accuracy of the arithmetic calculation is much less than the integer, for example, the result of 0.1+0.2 is not 0.3 but 0.30000000000000004 (not counting altogether 15 0). This small rounding error can result in the inability to test a specific floating-point value. Of course, if it's 0.15+0.15 or 0.05+0.25, then it's fine. Therefore, do not test specific floating-point values.

Range of values:

Due to memory limitations, ECMAScript does not save all the values in the world, and the minimum value is generally preserved:number.min_value , the maximum value is saved in Number.MAX_VALUE . The out-of-range translates into a special Infinity value, or -infinity (negative infinity)if it is negative, and positive value is Infinity (positive infinity). If a calculation returns infinity (either positive or negative), then this value cannot be calculated for the next time. So to avoid this, we can use the Isfinite () function to determine whether the function is in the maximum and minimum values. If it is, then return True. For the maximum and minimum values, the minimum value in most browsers can be:5e-324 and the maximum can be:1.7976931348623157e+308.

NaN (not a number):

 This value is used to indicate that an operand that would have returned a numeric value did not return a number. (Popular point is the return of this number is not a number). Dividing any number in another programming language by 0 may result in an error, but JavaScript does not, and it returns a Nan.

Nan has two characteristics: 1, any operation involving Nan (for example: NAN/5) will return Nan. 2, Nan is not equal to any number, even if this number is Nan also (Nan==nan will error).

For these two characteristics of Nan, ECMAScript defines the IsNaN () function, which receives a parameter whose arguments can be of any type, and this function can help us to determine that the value is "not a value", and that the IsNaN () will help us convert it to a value when it receives the parameter. The number that cannot be converted to a value returns TRUE. Like what:

Alert (IsNaN (NaN))//true

Alert (IsNaN)//false 10 is a numeric value

Alert (IsNaN ("ten"))//false can be converted to a number

Alert (IsNaN ("Blue"))//true cannot be converted to a number

Alert (IsNaN ("true"))//false can be converted to a value of 1

This function is incredibly useful for objects, and when an argument is an object, the ValueOf () method is called first, and then the return value is determined to be converted to a number, and if not, the ToString () method is called to test. (The details will be answered later, please be patient and look down).

Numeric conversions:

There are three functions that can convert a numeric value: Number (), parseint (), parsefloat (). The first function is for any type, and the other two functions are dedicated to exchanging strings for numeric values. These three functions have different outputs for the same parameters, so let's take a detailed look at them in the next step:

Number () Conversion rule:

1, if it is a Boolean. True and false are converted to 1 and 0.

2, if is the number (nonsense, if is the number that certainly did not change).

3, if NULL, returns 0.

4. If it is undefined, return nan.

5, if it is a string, then follow the following rules: (1) The string contains only numbers, so long to convert to the corresponding number. For example, "111" turns into 111, "012" turns 12 (the previous 0 is ignored).

(2) If the string contains a valid floating point, then the corresponding floating point such as "1.1" turns into 1.1.

(3) If the string contains a valid hexadecimal, such as "0xf" then it will be converted to the corresponding decimal number.

(4) If the string is "" null, then convert to 0.

(5) If the string contains characters other than the above-mentioned format, it is converted to Nan, and if it is an object, then the valueof () method of the object is called, then the test is judged again according to the rule above, and if the result of the conversion is Nan, then the ToString () method is called again for detection. (Have to say "object" treatment is not normal).

Instance: Number ("Hello")//NaN

Number ("")//0

Number ("00064")//64

Number (TRUE)//1

parseint ():

This method features: 1, the space before the string is ignored until the first character is found.

2. If the first character is not a numeric character or a minus sign, Nan is returned.

3. This method will parse the numeric string until it encounters a non-numeric character.

4, the method can be recognized in the system, such as the beginning of 0x will be as hexadecimal. 0 begins with octal (but there are differences here, ECMAScript 3 is considered octal, and ECMAScript is considered a decimal 0).

Instance:

Paeseint ("123ABC")//123

Paeseint ("")//NaN

Paeseint ("0xA")//10

Paeseint (22.5)//22

Paeseint ("070")//56

Paeseint ("70")//70

Paeseint ("0xf")//15

As said above. Because 070 does not resolve the same in different versions, this can result in bad results. As a result, we can use this method in the usual two parameters. The second parameter specifies a binary. For example parseint ("070", 8). This makes it possible to obtain the desired effect more accurately. (the second parameter can be ignored in decimal).

Parsefloat ():

This method does not repeat, because it is similar to parseint but there are still different situations, such as: parsefloat will always ignore the leading 0, hexadecimal format characters will be converted to 0, this method only resolves decimal, so do not provide a second argument, directly on the instance:

Parsefloat ("1234blue")//1234

Parsefloat ("0xA")//0 hex is parsed into 0

Parsefloat ("22.5")//22.5

parsefloat ("22.34.5")//22.34

parsefloat ("0908.5")//908.5

Parsefloat ("3.125e7")//31250000.

"JavaScript Advanced Programming" Learning summary three (2)

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.