Back to Biomejs

useNumberToFixedDigitsArgument

src/content/docs/linter/rules/use-number-to-fixed-digits-argument.mdx

latest3.4 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.8.0` - Diagnostic Category: [`lint/suspicious/useNumberToFixedDigitsArgument`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`unicorn/require-number-to-fixed-digits-argument`](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/require-number-to-fixed-digits-argument.md)

How to configure

json
{
	"linter": {
		"rules": {
			"suspicious": {
				"useNumberToFixedDigitsArgument": "error"
			}
		}
	}
}

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

Invalid

js
const string = number.toFixed();
<pre class="language-text"><code class="language-text">code-block.js:1:30 <a href="https://biomejs.dev/linter/rules/use-number-to-fixed-digits-argument">lint/suspicious/useNumberToFixedDigitsArgument</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Specify the number of digits you want to appear after the decimal point.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const string = number.toFixed(); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Unsafe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Add explicit digits argument to </span><span style="color: lightgreen;"><strong>toFixed</strong></span><span style="color: lightgreen;"> method.</span> <strong> 1 │ </strong>const<span style="opacity: 0.8;">·</span>string<span style="opacity: 0.8;">·</span>=<span style="opacity: 0.8;">·</span>number.toFixed(<span style="color: MediumSeaGreen;">0</span>); <strong> │ </strong> <span style="color: MediumSeaGreen;">+</span> </code></pre>

Valid

js
const string = foo.toFixed(0);
js
const string = foo.toFixed(2);

Caveats

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

</TabItem> </Tabs>