javascript学习笔记之类型转换测试
/* -------------------------
Javascript 各种类型转换测试
----------------------------*/
function writeline(message)
{
document.write(message);
document.write("<br>");
}
/*---转换成String----*/
//Boolean to string
var trueToStr = true;
var falseToStr = false;
writeline(trueToStr.toString()); //true
writeline(falseToStr.toString());//false
//Number to string
//默认模式 无论采用什么表示法声明数字,toString() 返回都是十进制表示
var num1 = 10;
var num2 = 10.0;
var num3 = 10e6;
writeline(num1.toString()); //10
writeline(num2.toString()); //10
writeline(num3); //1000000
//基模式
writeline(num1.toString(2)); //以二进制输出 1010
writeline(num2.toString(8)); //以八进制输出 12
writeline(num3.toString(16)); //以十六制输出 989680
/*------Number特定输出-------*/
var num_0 = 55;
var num_1 = 55.465;
var num_2 = 55.5143;
//固定输出 toFixed 参数是指定小数位,四舍五入
writeline("test toFixed
");
writeline(num_0.toFixed(2));//55.00
writeline(num_1.toFixed(2));//55.47
writeline(num_2.toFixed(2));//55.51
//科学计算法输出 参数指定输出位数,不包括指数位,
writeline("to Exponential
");
writeline(num_0.toExponential(1));//5.5e + 1
writeline(num_1.toExponential(1));//5.5e + 1 少于数字位数,也会舍入操作
writeline(num_2.toExponential(1));//5.6e + 1
writeline(num_0.toExponential(2));//5.50e+1
writeline(num_1.toExponential(2));//5.55e+1
writeline(num_2.toExponential(2));//5.55e+1
//自动返回数字最有意义的形式 toPrecision 参数指定数学输出位
var aNumber = 99;
writeline(aNumber.toPrecision(1));// 1e + 2
writeline(aNumber.toPrecision(2));// 99
writeline(aNumber.toPrecision(3));//99.0
//String()函数
writeline(String(null)); //null
/*----转换成Number----*/
//String to Number
//parseInt 转换成整数 把从0位置开始的所有有效数字字面量转换成整数,如果0位置非有效数字面量,返回NaN
writeline(parseInt("1234blue")); //1234
writeline(parseInt("0xA")); //10
writeline(parseInt("22.3")); //22
writeline(parseInt("22.8")); //22
writeline(parseInt("010")); //8
writeline(parseInt("blue")); //NaN
//parseInt基模式
writeline(parseInt("AF",16)); //175 相当于 parseInt("0xAF")
writeline(parseInt("101001",2)); //41
writeline(parseInt("10",8)); //8
//parseFloat 转换成浮点数 只认为浮点数字符串是十进制形式
writeline(parseFloat("1234blue")); //1234
writeline(parseFloat("0xA")); //0
writeline(parseFloat("22.3")); //22.3
writeline(parseFloat("22.8")); //22.8
writeline(parseFloat("010")); //10
writeline(parseFloat("22.34.5"));//22.34
writeline(parseFloat("1.1111111111111111111111111111111111111111111111111111111"));//1.1111111111111111
writeline(parseFloat("blue")); //NaN
//Number函数转换 检查的是整个值,如果整个字符串能完整转换,将自动调用parseInt或parseFloat
writeline(Number(false)); //0
writeline(Number(true)); //1
writeline(Number(undefined)); //NaN
writeline(Number(null)); //0
writeline(Number("5.5")); //5.5
writeline(Number("56")); //56
writeline(Number("5.6.7")); //NaN parseInt将返回5
writeline(Number("1234blue"));//NaN
writeline(Number("3e5")); //300000
//Boolean to Number
writeline(Number(true)); //1
writeline(Number(false));//0
/*---转换成Boolean----*/
//当要转换的值至少有一个字符,非0数字或对象,将返回true
//值是空字符串.数字0.undefined或null,将返回false
writeline(Boolean("abc")); //true
writeline(Boolean("123")); //true
writeline(Boolean(new Object()));//true
writeline(Boolean(''));//false
writeline(Boolean(undefined));//false
writeline(Boolean(null));//false
writeline(Boolean(0));//false
Javascript 各种类型转换测试
----------------------------*/
function writeline(message)
{
document.write(message);
document.write("<br>");
}
/*---转换成String----*/
//Boolean to string
var trueToStr = true;
var falseToStr = false;
writeline(trueToStr.toString()); //true
writeline(falseToStr.toString());//false
//Number to string
//默认模式 无论采用什么表示法声明数字,toString() 返回都是十进制表示
var num1 = 10;
var num2 = 10.0;
var num3 = 10e6;
writeline(num1.toString()); //10
writeline(num2.toString()); //10
writeline(num3); //1000000
//基模式
writeline(num1.toString(2)); //以二进制输出 1010
writeline(num2.toString(8)); //以八进制输出 12
writeline(num3.toString(16)); //以十六制输出 989680
/*------Number特定输出-------*/
var num_0 = 55;
var num_1 = 55.465;
var num_2 = 55.5143;
//固定输出 toFixed 参数是指定小数位,四舍五入
writeline("test toFixed
![](https://www.cnblogs.com/Images/dot.gif)
writeline(num_0.toFixed(2));//55.00
writeline(num_1.toFixed(2));//55.47
writeline(num_2.toFixed(2));//55.51
//科学计算法输出 参数指定输出位数,不包括指数位,
writeline("to Exponential
![](https://www.cnblogs.com/Images/dot.gif)
writeline(num_0.toExponential(1));//5.5e + 1
writeline(num_1.toExponential(1));//5.5e + 1 少于数字位数,也会舍入操作
writeline(num_2.toExponential(1));//5.6e + 1
writeline(num_0.toExponential(2));//5.50e+1
writeline(num_1.toExponential(2));//5.55e+1
writeline(num_2.toExponential(2));//5.55e+1
//自动返回数字最有意义的形式 toPrecision 参数指定数学输出位
var aNumber = 99;
writeline(aNumber.toPrecision(1));// 1e + 2
writeline(aNumber.toPrecision(2));// 99
writeline(aNumber.toPrecision(3));//99.0
//String()函数
writeline(String(null)); //null
/*----转换成Number----*/
//String to Number
//parseInt 转换成整数 把从0位置开始的所有有效数字字面量转换成整数,如果0位置非有效数字面量,返回NaN
writeline(parseInt("1234blue")); //1234
writeline(parseInt("0xA")); //10
writeline(parseInt("22.3")); //22
writeline(parseInt("22.8")); //22
writeline(parseInt("010")); //8
writeline(parseInt("blue")); //NaN
//parseInt基模式
writeline(parseInt("AF",16)); //175 相当于 parseInt("0xAF")
writeline(parseInt("101001",2)); //41
writeline(parseInt("10",8)); //8
//parseFloat 转换成浮点数 只认为浮点数字符串是十进制形式
writeline(parseFloat("1234blue")); //1234
writeline(parseFloat("0xA")); //0
writeline(parseFloat("22.3")); //22.3
writeline(parseFloat("22.8")); //22.8
writeline(parseFloat("010")); //10
writeline(parseFloat("22.34.5"));//22.34
writeline(parseFloat("1.1111111111111111111111111111111111111111111111111111111"));//1.1111111111111111
writeline(parseFloat("blue")); //NaN
//Number函数转换 检查的是整个值,如果整个字符串能完整转换,将自动调用parseInt或parseFloat
writeline(Number(false)); //0
writeline(Number(true)); //1
writeline(Number(undefined)); //NaN
writeline(Number(null)); //0
writeline(Number("5.5")); //5.5
writeline(Number("56")); //56
writeline(Number("5.6.7")); //NaN parseInt将返回5
writeline(Number("1234blue"));//NaN
writeline(Number("3e5")); //300000
//Boolean to Number
writeline(Number(true)); //1
writeline(Number(false));//0
/*---转换成Boolean----*/
//当要转换的值至少有一个字符,非0数字或对象,将返回true
//值是空字符串.数字0.undefined或null,将返回false
writeline(Boolean("abc")); //true
writeline(Boolean("123")); //true
writeline(Boolean(new Object()));//true
writeline(Boolean(''));//false
writeline(Boolean(undefined));//false
writeline(Boolean(null));//false
writeline(Boolean(0));//false