跳转到内容

useNumberToFixedDigitsArgument (since v1.8.0)

Diagnostic Category: lint/nursery/useNumberToFixedDigitsArgument

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/nursery/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.