Skip to content
← Back to rules

unicorn/prefer-math-trunc 严谨

An auto-fix is available for this rule.

它的作用

推荐使用 Math.trunc() 代替位运算,以提高代码清晰度并获得更可靠的结果。

该规则禁止使用以下位运算:

为什么这是不好的?

使用位运算来截断数字不够清晰,并且在某些情况下无法正常工作 一些情况

示例

此规则的错误代码示例:

javascript
const foo = 1.1 | 0;

此规则的正确代码示例:

javascript
const foo = Math.trunc(1.1);

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "rules": {
    "unicorn/prefer-math-trunc": "error"
  }
}
bash
oxlint --deny unicorn/prefer-math-trunc

参考资料