TypeScript - 数字
TypeScript - 数字
Section titled “TypeScript - 数字”TypeScript 中的 number 类型表示数值。与其他一些具有用于整数(如 int)和浮点数(如 float 或 double)的独立类型的语言不同,TypeScript 的 number 类型同时包含这两种。
在内部,它对应于 JavaScript 标准的双精度 64 位浮点格式(IEEE 754 标准)。
let integerValue: number = 42;let floatValue: number = 3.14159;let hexValue: number = 0xf00d; // 十六进制let binaryValue: number = 0b1010; // 二进制let octalValue: number = 0o744; // 八进制let bigIntValue: bigint = 9007199254740991n; // 注意: bigint 是一个单独的类型
console.log(integerValue); // 42console.log(floatValue); // 3.14159console.log(hexValue); // 61453console.log(binaryValue); // 10console.log(octalValue); // 484console.log(bigIntValue); // 9007199254740991nNumber 对象及其属性
Section titled “Number 对象及其属性”虽然您通常使用原始 number 值,但 JavaScript(以及 TypeScript)也有内置的 Number 对象构造函数。此对象提供了与数字相关的有用静态属性:
| 属性 | 描述 |
|---|---|
Number.MAX_VALUE | 最大的正数表示值。 |
Number.MIN_VALUE | 最小的正数表示值(最接近零)。 |
Number.NaN | 表示“Not-a-Number”(非数字)—— 无效数学运算的结果(例如 0 / 0)。 |
Number.NEGATIVE_INFINITY | 表示负无穷大的特殊值。 |
Number.POSITIVE_INFINITY | 表示正无穷大的特殊值。 |
Number.EPSILON | (ES6)1 与大于 1 的最小浮点数之间的差。 |
Number.MAX_SAFE_INTEGER | (ES6)不丢失精度的情况下可以安全表示的最大整数(2^53 - 1)。 |
Number.MIN_SAFE_INTEGER | (ES6)不丢失精度的情况下可以安全表示的最小整数(-(2^53 - 1))。 |
示例:Number 属性
Section titled “示例:Number 属性”console.log("TypeScript Number Properties:");console.log(`Max Value: ${Number.MAX_VALUE}`);console.log(`Min Value: ${Number.MIN_VALUE}`);console.log(`Negative Infinity: ${Number.NEGATIVE_INFINITY}`);console.log(`Positive Infinity: ${Number.POSITIVE_INFINITY}`);console.log(`NaN: ${Number.NaN}`);console.log(`Max Safe Integer: ${Number.MAX_SAFE_INTEGER}`);处理 NaN
Section titled “处理 NaN”NaN 是一个特殊的数值,表示无法表示的结果。一个关键特性是 NaN 不等于任何值,包括它自己(NaN === NaN 为 false)。
要检查一个值是否为 NaN,可以使用全局的 isNaN() 函数或更健壮的 Number.isNaN() 方法(推荐,因为它不会强制转换非数字值)。
let result1: number = Math.sqrt(-1); // 结果为 NaNlet result2: number = parseInt("not a number"); // 结果为 NaNlet result3: number = 0 / 0; // 结果为 NaN
console.log(`Result 1: ${result1}`); // NaN
console.log(`result1 === NaN: ${result1 === Number.NaN}`); // false!
console.log(`isNaN(result1): ${isNaN(result1)}`); // trueconsole.log(`Number.isNaN(result1): ${Number.isNaN(result1)}`); // true (推荐)
console.log(`Number.isNaN("hello"): ${Number.isNaN("hello")}`); // falseconsole.log(`isNaN("hello"): ${isNaN("hello")}`); // true (isNaN 会将 "hello" 强制转换为 NaN)常用的数字方法
Section titled “常用的数字方法”数字值(基本类型)可以访问 Number.prototype 中的方法。
| 方法 | 描述 | 示例 |
|---|---|---|
toExponential(fractionDigits?) | 返回以指数记法表示数字的字符串。 | (12345).toExponential(2) 是 '1.23e+4' |
toFixed(digits?) | 返回以定点记法表示数字的字符串(具有指定的小数位数)。 | (3.14159).toFixed(2) 是 '3.14' |
toLocaleString(locales?, options?) | 返回数字的语言敏感字符串表示形式。 | (123456.789).toLocaleString('de-DE') 可能是 '123.456,789' |
toPrecision(precision?) | 返回以指定总有效数字表示数字的字符串。 | (123.456).toPrecision(4) 是 '123.5' |
toString(radix?) | 返回以指定基数(radix)表示数字的字符串。默认基数为 10。 | (255).toString(16) 是 'ff',(10).toString(2) 是 '1010' |
valueOf() | 返回数字对象的原始值(较少直接使用)。 | let n = new Number(10); n.valueOf() 是 10 |
示例:数字方法
Section titled “示例:数字方法”let pi: number = 3.1415926535;let largeNum: number = 1_234_567.89; // 使用下划线提高可读性
console.log(`pi.toFixed(3): ${pi.toFixed(3)}`); // 3.142console.log(`largeNum.toExponential(1): ${largeNum.toExponential(1)}`); // 1.2e+6console.log(`largeNum.toLocaleString('en-US'): ${largeNum.toLocaleString('en-US')}`); // 1,234,567.89console.log(`(15).toString(16): ${(15).toString(16)}`); // fconsole.log(`(15).toString(2): ${(15).toString(2)}`); // 1111Number vs. number
Section titled “Number vs. number”始终优先使用原始类型 number 进行注解(例如 let x: number)。避免使用对象类型 Number(例如 let x: Number),除非您有特定的原因,因为它指的是对象包装器类型,而不是原始类型。