Skip to content

useNumberToFixedDigitsArgument

诊断类别:lint/suspicious/useNumberToFixedDigitsArgument

¥Diagnostic Category: lint/suspicious/useNumberToFixedDigitsArgument

自从:v1.8.0

¥Since: v1.8.0

来源:

¥Sources:

强制使用带有 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