TypeScript Number 知识点大全
编程小白也能轻松掌握的TypeScript数值类型详解
Number类型基础
TypeScript里的Number类型就是用来表示数值的,无论是整数还是小数。
声明Number变量
// 声明数值变量
let age: number = 30;
let price: number = 9.99;
let hexValue: number = 0xf00d; // 十六进制表示
let binaryValue: number = 0b1010; // 二进制表示
let octalValue: number = 0o744; // 八进制表示
let age: number = 30;
let price: number = 9.99;
let hexValue: number = 0xf00d; // 十六进制表示
let binaryValue: number = 0b1010; // 二进制表示
let octalValue: number = 0o744; // 八进制表示
提示:TypeScript和JavaScript一样,所有数字都是浮点数(64位双精度浮点数)。即使是整数,底层也是浮点数存储的。
数值范围与精度
安全整数范围
JavaScript/TypeScript能精确表示的整数范围是:
Number.MIN_SAFE_INTEGER; // -9007199254740991
Number.MAX_SAFE_INTEGER; // 9007199254740991
Number.MAX_SAFE_INTEGER; // 9007199254740991
注意:超出这个范围的整数计算可能会不准确!如果需要处理大整数,可以使用BigInt类型。
特殊数值
let notANumber: number = NaN; // 不是数字
let infinite: number = Infinity; // 无穷大
let negativeInfinite: number = -Infinity; // 负无穷大
let infinite: number = Infinity; // 无穷大
let negativeInfinite: number = -Infinity; // 负无穷大
常用方法和属性
Number对象属性
Number.MAX_VALUE; // 最大数值
Number.MIN_VALUE; // 最小数值
Number.POSITIVE_INFINITY; // 正无穷大
Number.NEGATIVE_INFINITY; // 负无穷大
Number.NaN; // 非数字值
Number.MIN_VALUE; // 最小数值
Number.POSITIVE_INFINITY; // 正无穷大
Number.NEGATIVE_INFINITY; // 负无穷大
Number.NaN; // 非数字值
常用方法
// 检查是否为整数
Number.isInteger(5); // true
// 检查是否为安全整数
Number.isSafeInteger(Number.MAX_SAFE_INTEGER); // true
// 解析字符串为浮点数
Number.parseFloat(‘10.5abc’); // 10.5
// 解析字符串为整数
Number.parseInt(‘10.5’); // 10
Number.isInteger(5); // true
// 检查是否为安全整数
Number.isSafeInteger(Number.MAX_SAFE_INTEGER); // true
// 解析字符串为浮点数
Number.parseFloat(‘10.5abc’); // 10.5
// 解析字符串为整数
Number.parseInt(‘10.5’); // 10
数值转换
在TypeScript中,将其他类型转换为Number类型有多种方式:
使用Number()函数
Number(‘123’); // 123
Number(‘123abc’); // NaN
Number(true); // 1
Number(false); // 0
Number(null); // 0
Number(undefined); // NaN
Number(‘123abc’); // NaN
Number(true); // 1
Number(false); // 0
Number(null); // 0
Number(undefined); // NaN
使用parseInt()和parseFloat()
parseInt(’10’); // 10
parseInt(‘10.5’); // 10
parseInt(’10px’); // 10
parseFloat(‘10.5’); // 10.5
parseFloat(‘10.5em’); // 10.5
parseInt(‘10.5’); // 10
parseInt(’10px’); // 10
parseFloat(‘10.5’); // 10.5
parseFloat(‘10.5em’); // 10.5
注意: parseInt() 总是返回整数,parseFloat() 可以返回小数。如果字符串开头不是数字,它们都会返回NaN。
数值运算
基本算术运算
let sum = 5 + 10; // 15
let difference = 20 – 5; // 15
let product = 5 * 3; // 15
let quotient = 15 / 3; // 5
let remainder = 15 % 4; // 3 (取余)
let difference = 20 – 5; // 15
let product = 5 * 3; // 15
let quotient = 15 / 3; // 5
let remainder = 15 % 4; // 3 (取余)
指数运算
let squared = 5 ** 2; // 25 (5的平方)
let cubed = 2 ** 3; // 8 (2的立方)
let cubed = 2 ** 3; // 8 (2的立方)
Math对象方法
Math.round(4.7); // 5 (四舍五入)
Math.ceil(4.3); // 5 (向上取整)
Math.floor(4.7); // 4 (向下取整)
Math.abs(-5.5); // 5.5 (绝对值)
Math.sqrt(64); // 8 (平方根)
Math.random(); // 0到1之间的随机数
Math.ceil(4.3); // 5 (向上取整)
Math.floor(4.7); // 4 (向下取整)
Math.abs(-5.5); // 5.5 (绝对值)
Math.sqrt(64); // 8 (平方根)
Math.random(); // 0到1之间的随机数
数值格式化
保留小数
let num = 123.456789;
num.toFixed(2); // “123.46” (保留两位小数)
num.toFixed(0); // “123” (四舍五入到整数)
num.toFixed(5); // “123.45679”
num.toFixed(2); // “123.46” (保留两位小数)
num.toFixed(0); // “123” (四舍五入到整数)
num.toFixed(5); // “123.45679”
指数表示法
let bigNum = 123456789;
bigNum.toExponential(2); // “1.23e+8”
let smallNum = 0.000123;
smallNum.toExponential(2); // “1.23e-4”
bigNum.toExponential(2); // “1.23e+8”
let smallNum = 0.000123;
smallNum.toExponential(2); // “1.23e-4”
数字分隔符
// 使用下划线提高大数字的可读性
let billion = 1_000_000_000;
let binary = 0b1010_0001_1000;
let hex = 0xA0_B0_C0;
console.log(billion); // 1000000000
let billion = 1_000_000_000;
let binary = 0b1010_0001_1000;
let hex = 0xA0_B0_C0;
console.log(billion); // 1000000000
数字类型最佳实践
- 尽量使用整数进行计算,避免浮点数精度问题
- 涉及货币计算时,使用整数(以分为单位)或使用专门的库
- 比较浮点数时,考虑使用容差范围而不是直接相等比较
- 大整数使用BigInt类型
- 使用数字分隔符提高大数字的可读性
常见陷阱
- 浮点数精度问题:0.1 + 0.2 !== 0.3
- parseInt转换时忘记指定进制
- 混淆数字和字符串:”5″ + 2 = “52” 而不是 7
- NaN与任何值(包括自身)比较都不相等
- 超出安全整数范围导致精度丢失