useNumberToFixedDigitsArgument
¥Summary
-
规则生效日期:
v1.8.0¥Rule available since:
v1.8.0 -
诊断类别:
lint/suspicious/useNumberToFixedDigitsArgument¥Diagnostic Category:
lint/suspicious/useNumberToFixedDigitsArgument -
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "suspicious": { "useNumberToFixedDigitsArgument": "error" } } }}¥Description
强制使用带有 Number#toFixed() 的数字参数。
¥Enforce using the digits argument with Number#toFixed().
使用 Number#toFixed() 时,请明确指定你希望小数点后出现的数字位数,以避免出现意外结果,而不是依赖其默认值 0。
¥When using Number#toFixed() explicitly specify the number of digits you want to appear after the decimal point,
to avoid unexpected results, rather than relying on its default value of 0.
¥Examples
¥Invalid
const string = number.toFixed();code-block.js:1:30 lint/suspicious/useNumberToFixedDigitsArgument FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Specify the number of digits you want to appear after the decimal point.
> 1 │ const string = number.toFixed();
│ ^^
2 │
ℹ Unsafe fix: Add explicit digits argument to toFixed method.
1 │ const·string·=·number.toFixed(0);
│ +
¥Valid
const string = foo.toFixed(0);const string = foo.toFixed(2);¥Caveats
此规则始终假设 toFixed 是在数字上调用的。它不检查被调用者的类型。
¥This rule always assumes that toFixed is called on a number.
It does not check the type of the callee.
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号