Home > Web Front-end > JS Tutorial > body text

JavaScript Advanced Programming Reading Notes (4) Type Conversion in ECMAScript_Javascript Skills

WBOY
Release: 2016-05-16 17:55:58
Original
920 people have browsed it

2.7 Type conversion
1. Convert to string
All objects have a toString() method that can be converted into a string. It should be noted that the toString() method of the Number type has two modes, namely the default mode. In base mode, the toString() method in the default mode only uses the corresponding string to output the corresponding decimal numerical value. In the base mode, numbers can be output in different bases. Example:

Copy code The code is 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. Convert to numbers
ECMAScript provides two methods to convert non-numeric primitive values ​​into numbers: parseInt() and parseFloat(). It should be noted that parseInt() converts character by character, up to non-numeric characters. The sample program is as follows:
Copy code The code is 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 mode, which can convert binary, octal, hexadecimal or any other base string into an integer. The base is specified by the second parameter of parseInt(). The example is as follows:
Copy code The code is 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 leading 0s, use radix 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, the others are roughly the same as parseInt(), example:
Copy code The code is 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. Forced type conversion
The forced type conversions available in ECMAScript are as follows:
Boolean(value)—— Convert the given value to Boolean type
Number (value) - Convert the given value into a number (can be an integer or floating point number)
String (value) - Convert the given value into a character String
Example:
Copy code The code is 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 Xingjian, self-improvement
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template