JavaScript Advanced Programming reading notes (iv) type conversion _javascript techniques in ECMAScript

Source: Internet
Author: User
Tags numeric value
2.7 Type Conversions
1. Convert to String
All objects have a ToString () method that can be converted to a string, note the ToString () method of the number type, which has two modes, the default mode and the base mode, and the default mode ToString () method simply outputs the corresponding 10-digit numeric value with the corresponding string , you can use a base pattern to output numbers in different bases. Example:
Copy Code code as follows:

var inum1=10;
var inum2=10.0;
var inum3=10;
Alert (inum1.tostring ()); Outpus "10"
Alert (inum2.tostring ()); Outpus "10"
Alert (inum3.tostring (2)); Outpus "1010"
Alert (inum3.tostring (8)); Outpus "12"
Alert (inum3.tostring (16)); Outpus "A"

2, converted into digital
ECMAScript provides two methods for converting non-numeric raw values to numbers: parseint () and parsefloat (), and note that parseint () is converted one character to the other, up to a non-numeric character. The sample program is as follows:
Copy Code code as follows:

var inum1=parseint ("1234blue"); Returns 1234
var inum2=parseint ("0xA"); Returns 10
var inum3=parseint ("22.5"); Returns 22
var inum4=parseint ("Blue"); Returns NaN

The parseint () method also has a base pattern that converts binary, octal, hexadecimal, or any other string into integers. The base is specified by the second parameter of parseint (), as shown in the following example:
Copy Code code as follows:

var inum1=parseint ("AF", 16); Returns 175
var inum2=parseint ("10", 2); Returns 2
var inum3=parseint ("10", 8); Returns 8
var inum4=parseint ("10", 10); Returns 10
Note: If the decimal number has a leading 0 to use cardinality mode
var inum5=parseint ("010"); Returns 8
var inum6=parseint ("010", 8); Returns 8
var inum7=parseint ("010", 10); Returns 10

Parasefloat () has no base mode, others are approximately the same as parseint (), Example:
Copy Code code as follows:

var fnum1=parasefloat ("1234blue"); Returns 1234.0
var fnum2=parasefloat ("0xA"); Returns NaN
var fnum3=parasefloat ("22.5"); Returns 22.5
var fnum4=parasefloat ("22.34.5"); Returns 22.34
var fnum5=parasefloat ("0908"); Returns 908
var fnum6=parasefloat ("Blue"); Returns NaN

3. Force type Conversion
The mandatory type conversions available in ECMAScript are as follows:
Boolean (value)--converts a given value to a Boolean
Number (value)-Converts a given value to a digit (can be an integer or float)
String (value)--converts a given value to a string
Example:
Copy Code code as follows:

var B1=boolean (""); False-empty string
var b2=boolean ("Hi"); True-not empty string
var b3=boolean (100); True-not Zero Number
var b4=boolean (NULL); False-null
var b5=boolean (0); False-zero
var B6=boolean (new Object ()); True Object
var i1=number (false); 0
var i2=number (true); 1
var i3=number (undefined); NaN
var i4=number (NULL); 0
var f5=number ("5.5"); 5.5
var i6=number ("56"); 56
var i7=number ("5.6.7"); NaN
var i8=number (new Object ()); NaN
var i9=number (100); 100

Author: Tian Xing Jian, self-improvement
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.