Skip to content

Latest commit

Β 

History

History
48 lines (35 loc) Β· 1.41 KB

require-number-to-fixed-digits-argument.md

File metadata and controls

48 lines (35 loc) Β· 1.41 KB

require-number-to-fixed-digits-argument

πŸ“ 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.

Examples

// ❌
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