如何以科学计数法格式化数字?
学习在 JavaScript 中显示极大和极小的数字
引言
科学计数法将数字表示为系数与 10 的幂的乘积。例如,123,000,000 可写作 1.23 × 10^8,0.000000456 可写作 4.56 × 10^-7。这种格式让极大或极小的数字更易于阅读和比较。
展示科学数据、财务信息或技术测量值的 Web 应用通常需要以这种方式格式化数字。JavaScript 通过 Intl.NumberFormat API 内置支持科学计数法,能够处理格式化和本地化。
本文将介绍如何以科学计数法格式化数字、控制精度、理解不同地区的差异,并在科学计数法与工程计数法之间进行选择。
以科学计数法格式化数字
Intl.NumberFormat 构造函数接受 notation 选项,用于控制数字的显示方式。将 notation 设置为 "scientific" 时,数字将以科学计数法格式化。
const formatter = new Intl.NumberFormat('en-US', {
notation: 'scientific'
});
console.log(formatter.format(987654321));
// Output: 9.877E8
输出 9.877E8 表示 9.877 × 10^8。E 用于分隔系数和指数部分。
格式化极大的数字
科学计数法通过将数字表示为系数和指数,简化了极大数字的显示。
const formatter = new Intl.NumberFormat('en-US', {
notation: 'scientific'
});
console.log(formatter.format(6.02214076e23));
// Output: 6.022E23
console.log(formatter.format(299792458));
// Output: 2.998E8
console.log(formatter.format(7800000000));
// Output: 7.8E9
数字 6.02214076e23(阿伏伽德罗常数)格式化后为 6.022E23,比 602,214,076,000,000,000,000,000 更易于阅读。
格式化极小的数字
科学计数法同样适用于极小的数字,使用负指数来表示小于 1 的数值。
const formatter = new Intl.NumberFormat('en-US', {
notation: 'scientific'
});
console.log(formatter.format(0.000000001));
// Output: 1E-9
console.log(formatter.format(0.00000000000000000016));
// Output: 1.6E-19
console.log(formatter.format(0.0000000456));
// Output: 4.56E-8
数字 0.00000000000000000016(约为一个电子的电荷,单位库仑)格式化后为 1.6E-19,避免了数零的麻烦。
科学计数法的格式因地区而异
不同的地区在科学计数法中使用不同的小数分隔符。虽然在大多数基于拉丁字母的地区,指数分隔符通常为 E,但系数中的小数分隔符会根据本地习惯而变化。
const numberToFormat = 987654321;
// US English uses a period as the decimal separator
const usFormatter = new Intl.NumberFormat('en-US', {
notation: 'scientific'
});
console.log(usFormatter.format(numberToFormat));
// Output: 9.877E8
// Portuguese uses a comma as the decimal separator
const ptFormatter = new Intl.NumberFormat('pt-PT', {
notation: 'scientific'
});
console.log(ptFormatter.format(numberToFormat));
// Output: 9,877E8
// German also uses a comma as the decimal separator
const deFormatter = new Intl.NumberFormat('de-DE', {
notation: 'scientific'
});
console.log(deFormatter.format(numberToFormat));
// Output: 9,877E8
美国英语地区会使用句点 9.877E8,而葡萄牙语和德语地区则会用逗号 9,877E8。
控制科学计数法的精度
minimumSignificantDigits 和 maximumSignificantDigits 选项用于控制格式化输出中显示的有效数字位数。有效数字包括系数中从第一个非零数字到最后一个数字的所有数字。
const formatter = new Intl.NumberFormat('en-US', {
notation: 'scientific',
minimumSignificantDigits: 3,
maximumSignificantDigits: 3
});
console.log(formatter.format(123456789));
// Output: 1.23E8
console.log(formatter.format(0.00045678));
// Output: 4.57E-4
将这两个选项都设置为 3 时,输出将严格限制为三位有效数字。如果不设置这些选项,格式化器会包含原始数字中的更多位数。
你还可以分别设置最小值和最大值,以允许可变精度。
const formatter = new Intl.NumberFormat('en-US', {
notation: 'scientific',
minimumSignificantDigits: 2,
maximumSignificantDigits: 5
});
console.log(formatter.format(123456789));
// Output: 1.2346E8
console.log(formatter.format(100000000));
// Output: 1.0E8
此配置允许根据输入数字,显示 2 到 5 位有效数字。
工程计数法格式化数字
工程计数法是科学计数法的一种变体,其指数始终为 3 的倍数。这与 SI 公制前缀(如 kilo(10^3)、mega(10^6)、giga(10^9))保持一致。
const formatter = new Intl.NumberFormat('en-US', {
notation: 'engineering'
});
console.log(formatter.format(987654321));
// Output: 987.654E6
console.log(formatter.format(0.000000456));
// Output: 456E-9
数字 987,654,321 在工程计数法下格式化为 987.654E6,而在科学计数法下则为 9.877E8。此处的指数 6 是 3 的倍数。
科学计数法与工程计数法的区别
科学计数法与工程计数法的主要区别在于指数的选择。科学计数法可以使用任意整数指数,而工程计数法则将指数限制为 3 的倍数。
const scientificFormatter = new Intl.NumberFormat('en-US', {
notation: 'scientific'
});
const engineeringFormatter = new Intl.NumberFormat('en-US', {
notation: 'engineering'
});
const testNumbers = [105900, 1234567, 0.00045];
testNumbers.forEach(num => {
console.log(`Number: ${num}`);
console.log(`Scientific: ${scientificFormatter.format(num)}`);
console.log(`Engineering: ${engineeringFormatter.format(num)}`);
console.log('---');
});
// Output:
// Number: 105900
// Scientific: 1.059E5
// Engineering: 105.9E3
// ---
// Number: 1234567
// Scientific: 1.235E6
// Engineering: 1.235E6
// ---
// Number: 0.00045
// Scientific: 4.5E-4
// Engineering: 450E-6
// ---
对于 105,900,科学计数法会生成 1.059E5,而工程计数法会生成 105.9E3。当科学计数法本身已经使用了 3 的倍数(例如 1,234,567 生成 1.235E6 时),两种计数法会得到相同的结果。
科学计数法与工程计数法的应用场景
科学计数法适用于那些数据展示时,注重可读性和一致性,而不是与公制前缀对齐的应用。
科学计数法适用于:
- 科学计算器和计算工具
- 天文应用(距离、质量、光度)
- 化学应用(分子质量、浓度)
- 物理仿真(粒子质量、电磁数值)
工程计数法适用于用户以公制前缀思考或需要口头交流数值的场景。
工程计数法适用于:
- 电子应用(以千欧为单位的电阻、以皮法为单位的电容)
- 电气工程工具(电压、电流、功率)
- 通信系统(以兆赫为单位的频率、以千兆比特为单位的数据速率)
- 机械工程(以千牛为单位的力、以牛·米为单位的扭矩)
选择哪种计数法取决于用户和领域。科学计数法为所有数字提供一致的格式,而工程计数法则更贴合工程师交流测量值的方式。