no-loss-of-precision
不允许失去精度的字面数字
在 配置文件 中使用来自 @eslint/js
的 recommended
配置可以启用此规则
由于 64 位浮点舍入,此规则将禁止使用在转换为 JS Number
时会在运行时丢失精度的数字字面。
¥This rule would disallow the use of number literals that lose precision at runtime when converted to a JS Number
due to 64-bit floating-point rounding.
规则详情
¥Rule Details
在 JS 中,Number
是根据 IEEE 754 标准 存储为双精度浮点数的。因此,数字只能保持一定数量的数字的准确性。如果程序员输入额外的数字,这些数字将在转换为 Number
类型时丢失,并导致意外行为。
¥In JS, Number
s are stored as double-precision floating-point numbers according to the IEEE 754 standard. Because of this, numbers can only retain accuracy up to a certain amount of digits. If the programmer enters additional digits, those digits will be lost in the conversion to the Number
type and will result in unexpected behavior.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-loss-of-precision: "error"*/
const a = 9007199254740993
const b = 5123000000000000000000000000001
const c = 1230000000000000000000000.0
const d = .1230000000000000000000000
const e = 0X20000000000001
const f = 0X2_000000000_0001;
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-loss-of-precision: "error"*/
const a = 12345
const b = 123.456
const c = 123e34
const d = 12300000000000000000000000
const e = 0x1FFFFFFFFFFFFF
const f = 9007199254740991
const g = 9007_1992547409_91
版本
此规则是在 ESLint v7.1.0 中引入。