教程 > typescript 教程 > 阅读:50

typescript number——迹忆客-ag捕鱼王app官网

typescript 与 javascript 类似,支持 number 对象。

number 对象是原始数值的包装对象。

语法

var num = new number(value);

注意: 如果一个参数值不能转换为一个数字将返回 nan (非数字值)。

number 对象属性

下表列出了 number 对象支持的属性:

序号 属性 描述
1 max_value 可表示的最大的数,max_value 属性值接近于 1.79e 308。大于 max_value 的值代表 "infinity"。
2 min_value 可表示的最小的数,即最接近 0 的正数 (实际上不会变成 0)。最大的负数是 -min_value,min_value 的值约为 5e-324。小于 min_value ("underflow values") 的值将会转换为 0。
3 nan 非数字值(not-a-number)。
4 negative_infinity 负无穷大,溢出时返回该值。该值小于 min_value。
5 positive_infinity 正无穷大,溢出时返回该值。该值大于 max_value。
6 prototype number 对象的静态属性。使您有能力向对象添加属性和方法。
7 constructor 返回对创建此对象的 number 函数的引用。

typescript

console.log("typescript number 属性: "); 
console.log("最大值为: "   number.max_value); 
console.log("最小值为: "   number.min_value); 
console.log("负无穷大: "   number.negative_infinity); 
console.log("正无穷大:"   number.positive_infinity);

编译以上代码,得到以下 javascript 代码:

javascript

console.log("typescript number 属性: ");
console.log("最大值为: "   number.max_value);
console.log("最小值为: "   number.min_value);
console.log("负无穷大: "   number.negative_infinity);
console.log("正无穷大:"   number.positive_infinity);

输出结果为:

typescript number 属性:
最大值为: 1.7976931348623157e 308
最小值为: 5e-324
负无穷大: -infinity
正无穷大:infinity

nan 实例

typescript

var month = 0 
if( month<=0 || month >12) { 
    month = number.nan 
    console.log("月份是:"  month) 
} else { 
    console.log("输入月份数值正确。") 
}

编译以上代码,得到以下 javascript 代码:

javascript

var month = 0;
if (month <= 0 || month > 12) {
    month = number.nan;
    console.log("月份是:"   month);
}
else {
    console.log("输入月份数值正确。");
}

输出结果为:

月份是:nan

prototype 实例

typescript

function employee(id:number,name:string) { 
    this.id = id 
    this.name = name 
} 
 
var emp = new employee(123,"admin") 
employee.prototype.email = "admin@jiyik.com" 
 
console.log("员工号: " emp.id) 
console.log("员工姓名: " emp.name) 
console.log("员工邮箱: " emp.email)

编译以上代码,得到以下 javascript 代码:

javascript

function employee(id, name) {
    this.id = id;
    this.name = name;
}
var emp = new employee(123, "admin");
employee.prototype.email = "admin@jiyik.com";
console.log("员工号: "   emp.id);
console.log("员工姓名: "   emp.name);
console.log("员工邮箱: "   emp.email);

输出结果为:

员工号: 123
员工姓名: admin
员工邮箱: admin@jiyik.com

number 对象方法

number对象 支持以下方法:

序号 方法 & 描述 实例
1 toexponential() 把对象的值转换为指数计数法。
//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() 把数字转换为字符串,使用指定的基数。数字的基数是 2 ~ 36 之间的整数。若省略该参数,则使用基数 10。
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

查看笔记

扫码一下
查看教程更方便
网站地图