JavaScript learning Experience

Source: Internet
Author: User

There are three functions in JS that can convert non-numeric types to numeric types: number (), parseint (), parsefloat ().

the first function, the transformation function number () , can be used for any data type, while the other two functions are specifically used to convert a string into a numeric value. These 3 functions will return different results for the same input.

Number ()The conversion rules for a function are as follows.
If it is a BooleanValue, Trueand falsewill be converted separately to1And0。
If it is a numeric value, it is simply passed in and returned.
If it is nullValue, which returns0。
If it's undefined,, Return NaN。
If it is a string, follow these rules:
If the string contains only numbers (including cases preceded by a plus or minus sign), it is converted to a decimal value, which is "1"
will become1, "123"will become123, while the "011"will become11(Note: The leading 0 is ignored);
If the string contains a valid floating-point format, such as "1.1", it is converted to the corresponding floating-point value (again, the
slightly leading 0);
If the string contains a valid hexadecimal format, such as"0xf", it is converted to a decimal integer of the same size
Numerical
0< Span style= "font-family: ' fzssjw--gb1-0 ';" The
If the string contains characters other than the above format, Then convert it to nan.
If it is an object, call the object's valueof () method, then transforms the returned value according to the previous rule. If you convert
tostring () method, The returned character

BecauseNumber ()Functions are complex and not reasonable when converting strings, so it is more common to work with integers
parseint ()Function.parseint ()When a function converts a string, it is more to see if it conforms to the numeric pattern. It will ignore the word
String before the first non-whitespace character is found. If the first character is not a numeric character or a minus sign,parseint ()
It will returnNaN; in other words, useparseint ()Converting an empty string will returnNaN(Number ()Return to NULL characters0)。 Such as
The first character is a numeric character,parseint ()Will continue parsing the second character until all subsequent characters have been resolved or
A non-numeric character. For example"1234blue"will be converted to1234 because "blue" " 22.5 "
will be converted to 22 parseint () decimal, octal, and hexadecimal numbers discussed earlier). That is, if the string is "0x" hexadecimal integer if the string is " 0 " and followed by a numeric character, it is parsed as an octal number.

var num1 = parseint ("10", 2); 2 (by binary parsing)
var num2 = parseint ("10", 8); 8 (parsing by octal)
var num3 = parseint ("10", 10); Ten (by decimal resolution)
var num4 = parseint ("10", 16); (parse by hexadecimal)

Not specifying cardinality means letting parseint () decide how to parse the input string, so in order to avoid error parsing, we built
Specify the cardinality in whatever circumstances.

Most of the times, we're going to parse the decimal value, so we'll always take ten as the second argument is
very necessary.
Andparseint ()function is similar,Parsefloat ()Also from the first character (position0) begins parsing each character. And
is also parsed until the end of the string, or until an invalid floating-point numeric character is encountered. In other words, the string
A decimal point is valid, and the second decimal point is invalid, so the string following it is ignored. For example,
"22.34.5"will be converted to22.34。
In addition to the first decimal point being valid,Parsefloat ()Andparseint ()The second difference is that it will always ignore the leading
of 0.Parsefloat ()You can identify all floating-point numeric formats discussed earlier, and also include decimal integer formats. But the hexadecimal lattice
String will always be converted to an0。 Becauseparsefloat () resolves only decimal values , so it does not specify a base with the second parameter
number usage. One final note: If the string contains a number that resolves to an integer (no decimal point, or
parsefloat () parsefloat () convert values.
var num1 = parsefloat ("1234blue");//1234 var num2 = parsefloat ("0xA");//0
var num3 = parsefloat ("22.5"); 22.5
var num4 = parsefloat ("22.34.5");//22.34
var NUM5 = parsefloat ("0908.5"); 908.5
var num6 = parsefloat ("3.125e7");//31250000


This article is from the "9196441" blog, please be sure to keep this source http://9206441.blog.51cto.com/9196441/1697748

JavaScript learning Experience

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.