Skip to content
← Back to rules

eslint/no-loss-of-precision Correctness

This rule is turned on by default.

作用

禁止数字字面量的精度丢失。

为什么这不好?

这可能会在某些情况下导致意外结果。 例如,在执行数学运算时。

在 JavaScript 中,数字根据 IEEE 754 标准以双精度浮点数存储。 因此,数字只能在一定数量的位数内保持精度。 如果程序员输入更多位数,这些位数在转换为 Number 类型时会丢失,并导致意外/不正确的行为。

示例

以下是此规则的错误代码示例:

javascript
var x = 2e999;
javascript
var x = 9007199254740993;
javascript
var x = 5123000000000000000000000000001;
javascript
var x = 1230000000000000000000000.0;
javascript
var x = 0x200000_0000000_1;

以下是此规则的正确代码示例:

javascript
var x = 12345;
javascript
var x = 123.456;
javascript
var x = 123.0;
javascript
var x = 123e34;
javascript
var x = 0x1fff_ffff_fff_fff;

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "no-loss-of-precision": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-loss-of-precision": "error",
  },
});
bash
oxlint --deny no-loss-of-precision

版本

此规则在 v0.0.7 中添加。

参考