TypeScript - 数字
TypeScript 像 JavaScript 一样支持将数值作为 Number 对象处理。Number 对象将数值字面量转换为 number class 的实例。Number class 作为一个包装器,使数值字面量可以像对象一样进行操作。
TypeScript 的 number type 表示数值。所有数字都以浮点值形式表示。TypeScript 还支持 ECMAScript 2015 中引入的 binary、octal 和 hexadecimal 数字。
在 TypeScript 中,我们可以创建 number 原始类型以及 Number 对象。
语法
我们可以使用变量名后跟冒号 (:) 然后是 number 来声明 number 类型的变量 −
let varName: number = value;
在上述语法中,我们声明了一个名为 varName 的 number 类型变量。这里 value 是任何数值,例如 decimal、binary、octal 或 hexadecimal 数字。
我们也可以创建 Number 对象。要创建 Number 对象,可以使用 Number() 构造函数,如下所示 −
var var_name = new Number(value)
如果向 Number 构造函数传递非数值参数,它将返回 NaN (Not a Number)。
类型 'Number' 是一个包装对象,但类型 'number' 是原始类型。尽可能使用 'number'。类型 'Number' 不能赋值给类型 'number'。
创建 Number 类型
在下面的示例中,我们创建了一个 number 类型的变量 count,并将其赋值为 10。
let count: number = 10; console.log(count);
编译后,将生成以下 JavaScript 代码。
let count = 10; console.log(count);
输出如下 −
10
我们还可以将 float、binary、octal 和 hexadecimal 值赋给 number 类型的变量。请看下面的 TypeScript 代码片段
let decNum: number = 10.6; // floating point number let binNum: number = 0b101001; // binary number let octNum: number = 0o45; // octal number let hexNum: number = 0x80fd; // hexadecimal number
创建 Number 对象
const count = new Number(10); console.log(count); console.log(typeof count);
编译后,将生成相同的 JavaScript 代码。
上述示例代码的输出如下
[Number: 10] Object
Number 属性
下表列出了 Number 对象的一组属性 −
| 序号 | 属性 & 描述 |
|---|---|
| 1. | MAX_VALUE JavaScript 中数字能表示的最大可能值 1.7976931348623157E+308。 |
| 2. | MIN_VALUE JavaScript 中数字能表示的最小可能值 5E-324。 |
| 3. | NaN 等于一个不是数字的值。 |
| 4. | NEGATIVE_INFINITY 小于 MIN_VALUE 的值。 |
| 5. | POSITIVE_INFINITY 大于 MAX_VALUE 的值。 |
| 6. | prototype Number 对象的静态属性。使用 prototype 属性可以在当前文档中为 Number 对象分配新的属性和方法。 |
| 7. | constructor 返回创建此对象实例的函数。默认情况下,这是 Number 对象。 |
示例
console.log("TypeScript Number Properties: ");
console.log("Maximum value that a number variable can hold: " + Number.MAX_VALUE);
console.log("The least value that a number variable can hold: " + Number.MIN_VALUE);
console.log("Value of Negative Infinity: " + Number.NEGATIVE_INFINITY);
console.log("Value of Negative Infinity:" + Number.POSITIVE_INFINITY);
编译后,将生成相同的 JavaScript 代码。
其输出如下 −
TypeScript Number Properties: Maximum value that a number variable can hold: 1.7976931348623157e+308 The least value that a number variable can hold: 5e-324 Value of Negative Infinity: -Infinity Value of Negative Infinity:Infinity
示例:NaN
var month = 0
if( month<=0 || month >12) {
month = Number.NaN
console.log("Month is "+ month)
} else {
console.log("Value Accepted..")
}
编译后,将生成相同的 JavaScript 代码。
其输出如下 −
Month is NaN
示例:prototype
function employee(id:number,name:string) {
this.id = id
this.name = name
}
var emp = new employee(123,"Smith")
employee.prototype.email = "smith@abc.com"
console.log("Employee's Id: "+emp.id)
console.log("Employee's name: "+emp.name)
console.log("Employee's Email ID: "+emp.email)
编译后,将生成以下 JavaScript 代码 −
//Generated by typescript 1.8.10
function employee(id, name) {
this.id = id;
this.name = name;
}
var emp = new employee(123, "Smith");
employee.prototype.email = "smith@abc.com";
console.log("Employee 's Id: " + emp.id);
console.log("Employee's name: " + emp.name);
console.log("Employee's Email ID: " + emp.email);
其输出如下 −
Employee's Id: 123 Emaployee's name: Smith Employee's Email ID: smith@abc.com
Number 方法
Number 对象仅包含每个对象定义的一部分默认方法。以下列出了一些常用的方法 −
| 序号 | 方法 & 描述 |
|---|---|
| 1. | toExponential()
强制数字以指数记法显示,即使该数字处于 JavaScript 通常使用标准记法的范围内。 |
| 2. | toFixed()
以小数点右边指定位数的格式格式化数字。 |
| 3. | toLocaleString()
返回当前数字的字符串值版本,其格式可能根据浏览器的本地设置而变化。 |
| 4. | toPrecision()
定义数字显示的总位数(包括小数点左右的位数)。负精度将抛出错误。 |
| 5. | toString()
返回数字值的字符串表示。该函数传入基数(radix),这是一个介于 2 和 36 之间的整数,指定用于表示数值值的进制。 |
| 6. | valueOf()
返回数字的原始值。 |