JS type conversion-string-to-integer, floating-point method, forced type conversion, etc.

Source: Internet
Author: User
Tags type casting

1. Conversion function:

JS provides two conversion functions for parseint () and parsefloat (). The former converts the value into an integer, which converts the value into a floating-point number. Only these methods are called on the string type, and the two functions function correctly, and all other types return nan (not a number). The result of these two conversion functions is to convert the string data type to Number.

parseint () and Parsefloat () will parse the string carefully before judging whether the string is a numeric Value. The parseint () method first looks at the character at position 0, determines if it is a valid number, and if not, the method returns nan, and no further action is Taken. however, If the character is a valid number, the method will look at the character at position 1 and perform the same test. This process continues until a character is found that is not a valid number, at which point the parseint () converts the string before the character to a number.

For example, if you want to convert the string "1234blue" to an integer, then parseint () returns 1234 because when it detects the character b, it stops the detection process. The numeric literals contained in the string are correctly converted to numbers, so the string "0xA" is correctly converted to the number 10. however, the string "22.5" is converted to 22 because the decimal point is an invalid character for Integers. Some examples are as follows:

parseint ("1234blue"); Returns 1234
parseint ("0xA"); Returns 10
parseint ("22.5"); Returns 22
parseint ("blue"); Returns NaN

The parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other binary string into Integers. The base is specified by the second parameter of the parseint () method, so to parse the hexadecimal value, you call the parseint () method as Follows:
parseint ("AF", 16); Returns 175
of course, for binary, octal, or even decimal (the default mode), You can call the parseint () method this way:
parseint ("10", 2); Returns 2
parseint ("10", 8); Returns 8
parseint ("10", 10); Returns 10
If the decimal number contains a leading 0, it is best to use cardinality 10 so that the octal value is not unexpectedly obtained. For example:
parseint ("010"); Returns 8
parseint ("010", 8); Returns 8
parseint ("010", 10); Returns 10
In this code, two lines of code parse the string "010" into a number. The first line of code considers this string as an octal value, and parses it in the same way as the second line of code (the declaration base is 8). The last line of code declares that the cardinality is 10, so iNum3 finally equals 10.

The parsefloat () method is similar to the parseint () method, viewing each character starting at position 0 until the first non-valid character is found, and then converting the string before the character to a number. however, for this method, the first decimal point that appears is a valid character. If there are two decimal points, the second decimal point is considered invalid, and the parsefloat () method converts the string before the decimal point to a number. This means that the string "22.34.5" will be parsed into 22.34.
Another difference between using the Parsefloat () method is that the string must represent a floating-point number in decimal form, not in octal or hexadecimal form. The
Method ignores the leading 0, so the octal number 0908 is resolved to 908. For hexadecimal 0xA, The method returns Nan because, in a floating-point number, x is not a valid character. In addition, parsefloat () has no base mode.

The following is an example of using the parsefloat () method:
Parsefloat ("1234blue"); Returns 1234.0
Parsefloat ("0xA"); Returns NaN
Parsefloat ("22.5"); Returns 22.5
Parsefloat ("22.34.5"); Returns 22.34
Parsefloat ("0908"); Returns 908
Parsefloat ("blue"); Returns NaN

2. Forcing type conversions

You can also use the coercion type conversion (type Casting) to handle the type of the converted Value. A specific value can be accessed using a coercion type conversion, even if it is of a different type.
The 3 types of coercion conversions available in ECMAScript are as Follows:
Boolean (value)-converts the given value to a Boolean type;
Number (value)-converts The given value to a digit (which can be an integer or a floating point);
String (value)-converts the given value to a string.
Using one of these three functions to convert the value, a new value is created that holds the value converted directly from the original Value. This can cause unintended consequences.
The Boolean () function returns True when the value to be converted is a string of at least one character, a non-0 number, or an object (this is discussed in the next section). If the value is an empty string, the number 0, undefined, or null, it returns FALSE.

You can test a Boolean type cast with the following code Snippet.

Boolean (""); False–empty string
Boolean ("hi"); True–non-empty string
Boolean (100); True–non-zero number
Boolean (null); False-null
Boolean (0); False-zero
Boolean (new Object ()); True–object

The coercion type conversion of number () is similar to the parseint () and parsefloat () methods, except that it transforms the entire value, not the partial Value. remember, the parseint () and parsefloat () methods only convert the string before the first invalid character, so "4.5.6" is converted to "4.5". Coercion type conversion with number (), "4.5.6" returns Nan because the entire string value cannot be converted to Numbers. If the string value can be fully converted, number () will determine whether to call the parseint () method or call the parsefloat () method. The following table describes what happens when you call the number () method on different values:

Usage knot
Number (false) 0
Number (true) 1
Number (undefined) NaN
Number (null) 0
Number ("5.5") 5.5
Number ("56") 56
Number ("5.6.7") NaN
Number (new Object ()) NaN
Number (100) 100

The last method of forcing a type conversion string () is the simplest because it converts any value to a string. To perform this coercion type conversion, you only need to call the ToString () method that passes in the value as a parameter, that is, convert 1 to "1", convert True to "true", convert false to "false", and so On. The only difference between casting to a string and calling the ToString () method is that coercion of type conversions on null or undefined values can generate strings without throwing an error:

var S1 = String (null); "null"
var onull = null;
var s2 = onull.tostring (); Won ' t work, causes an error

3. Using JS variable weak type conversion

A small example, a look, you will understand.
<script>
var str= ' 012.345 ';
var x = str-0;
x = x*1;
</script>

The above example uses the characteristics of the weak type js, only the arithmetic operation, the implementation of the string to the number of the type conversion, but this method is not Recommended.

JS type conversion-string-to-integer, floating-point method, forced type conversion, etc.

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.