Aller au contenu

useNumberToFixedDigitsArgument

Ce contenu n’est pas encore disponible dans votre langue.

Diagnostic Category: lint/suspicious/useNumberToFixedDigitsArgument

Since: v1.8.0

Sources:

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.

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);

This rule always assumes that toFixed is called on a number. It does not check the type of the callee.