1. 整数数据类型
[javascript] view plaincopy
var intNum = 55; //integer
var octalNum1 = 070; //octal for 56
var octalNum2 = 079; //invalid octal - interpreted as 79
var octalNum3 = 08; //invalid octal - interpreted as 8
var hexNum1 = 0xA; //hexadecimal for 10
var hexNum2 = 0x1f; //hexedecimal for 31
2. 浮点数数据类型
[javascript] view plaincopy
var floatNum1 = 1.1;
var floatNum2 = 0.1;
var floatNum3 = .1; //valid, but not recommended
var floatNum1 = 1.; //missing digit after decimal - interpreted as integer1
var floatNum2 = 10.0; //whole number - interpreted as integer 10
var floatNum = 3.125e7; //equal to 31250000
3. NaN -- not a number
1)包含NaN值的任何运算的返回值均是NaN
2)NaN与任何其它数值都不相等,包括其它NaN
[javascript] view plaincopy
alert(NaN == NaN); //false
3)NaN判断:isNaN(),可对任何数据进行判断,任何不可被转换为数字的数据均返回true
[javascript] view plaincopy
alert(isNaN(NaN)); //true
alert(isNaN(10)); //false - 10 is a number
alert(isNaN(“10”)); //false - can be converted to number 10
alert(isNaN(“blue”)); //true - cannot be converted to a number
alert(isNaN(true)); //false - can be converted to number 1
当isNaN()的参数是对象时,先调用对象的valueOf()方法进行数字转换,如果无法转换为数字则继续调用对象的toString()方法进行数字转换来判断。
4. Number Conversions
1)Number()
[javascript] view plaincopy
var num1 = Number(“Hello world!”); //NaN
var num2 = Number(“”); //0
var num3 = Number(“000011”); //11
var num4 = Number(true); //1
2)parseInt()
[javascript] view plaincopy
var num1 = parseInt(“1234blue”); //1234
var num2 = parseInt(“”); //NaN
var num3 = parseInt(“0xA”); //10 - hexadecimal
var num4 = parseInt(22.5); //22
var num5 = parseInt(“70”); //70 - decimal
var num6 = parseInt(“0xf”); //15 - hexadecimal
var num = parseInt(“0xAF”, 16); //175
var num1 = parseInt(”AF”, 16); //175
var num2 = parseInt(”AF”); //NaN
var num1 = parseInt(“10”, 2); //2 - parsed as binary
var num2 = parseInt(“10”, 8); //8 - parsed as octal
var num3 = parseInt(“10”, 10); //10 - parsed as decimal
var num4 = parseInt(“10”, 16); //16 - parsed as hexadecimal
3)parseFloat()
[javascript] view plaincopy
var num1 = parseFloat(“1234blue”); //1234 - integer
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
源文来自中心
阅读(1562) | 评论(0) | 转发(0) |