π Enforce using the digits argument with Number#toFixed().
πΌ This rule is enabled in the following configs: β
recommended, βοΈ unopinionated.
π§ This rule is automatically fixable by the --fix CLI option.
When calling Number#toFixed(), always explicitly specify the digits argument. While the default is 0, explicitly stating it makes the code clearer and less ambiguous about the intended behavior.
// β
const string = number.toFixed();
// β
const string = number.toFixed(0);// β
const price = 19.99;
const displayPrice = price.toFixed(); // Unclear intent
// β
const price = 19.99;
const displayPrice = price.toFixed(2); // Currency, 2 decimal places// β
const average = 95.667;
const rounded = average.toFixed();
// β
const average = 95.667;
const rounded = average.toFixed(0); // Round to integer, intention is clear// β
const percentage = 87.567;
const display = percentage.toFixed(1); // Display with 1 decimal place