一、number
1)、语法:
var num = new Number(value);
2)、对象属性:MAX_VALUE、MIN_VALUE、NaN、NEGATIVE_INFINITY、POSITIVE_INFINITY、prototype、constructor
1、prototype
例如:
function em(id:number,name:string){
this.id = id;
this.name = name;
}
var em1 = new em(123,"admin")
em.prototype.email = "admin@runoob.com"
console.log("员工号码"+em1.id)
console.log("员工姓名"+em1.name)
console.log("员工邮箱"+em1.email)
结果:
员工号: 123
员工姓名: admin
员工邮箱: admin@runoob.com
2、NaN
var month = 0;
if(month <=0 || month > 12){
month = Number.NaN
console.log('月份是'+ month)
}else{
console.log("输入正确的")
}
结果:月份是:NaN
2)、对象方法:toExponential()、toFixed()、toLocaleString()、toPrecision()、toString()、valueOf()
1、toExponential()对象的值转换为指数计数法。
例如:
var num1 = 1225.30
var val = num1.toExponential();
console.log(val) // 输出: 1.2253e+3
2、toFixed()把数字转换为字符串,并对小数点指定位数。
例如:
var num3 = 177.234
console.log("num3.toFixed() 为 "+num3.toFixed()) // 输出:177
console.log("num3.toFixed(2) 为 "+num3.toFixed(2)) // 输出:177.23
console.log("num3.toFixed(6) 为 "+num3.toFixed(6)) // 输出:177.234000
3、toLocaleString()把数字转换为字符串,使用本地数字格式顺序。
例如:
var num = new Number(177.1234);
console.log( num.toLocaleString()); // 输出:177.1234
4、toPrecision()把数字格式化为指定的长度。
例如:
var num = new Number(7.123456);
console.log(num.toPrecision()); // 输出:7.123456
console.log(num.toPrecision(1)); // 输出:7
console.log(num.toPrecision(2)); // 输出:7.1
5、toString()把数字转换为字符串,使用指定的基数。
例如:
var num = new Number(10);
console.log(num.toString()); // 输出10进制:10
console.log(num.toString(2)); // 输出2进制:1010
console.log(num.toString(8)); // 输出8进制:12
6、valueOf()返回一个 Number 对象的原始数字值。
例如:
var num = new Number(10);
console.log(num.valueOf()); // 输出:10
网友评论