useNumberToFixedDigitsArgument
Diagnostic Category: lint/suspicious/useNumberToFixedDigitsArgument
Since: v1.8.0
Sources:
Description
Section titled “Description”Enforce using the digits argument with Number#toFixed()
.
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
Section titled “Examples”Invalid
Section titled “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);
│ +
const string = foo.toFixed(0);
const string = foo.toFixed(2);
Caveats
Section titled “Caveats”This rule always assumes that toFixed
is called on a number.
It does not check the type of the callee.
How to configure
Section titled “How to configure”{ "linter": { "rules": { "suspicious": { "useNumberToFixedDigitsArgument": "error" } } }}