# unicorn/numeric-separators-style

> Enforce how digits are grouped with `_` in numbers

`1000000` and `10000000` are hard to tell apart. `1_000_000` is easy to read, but only if the groups are all the same size. `10_00_000` is worse than no separators.

By default, decimal numbers of five digits or more are grouped by three, hexadecimal numbers by two, and binary and octal numbers by four. Groups are counted from the right.

`--fix` rewrites the number.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/numeric-separators-style" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"unicorn/numeric-separators-style" = ["error", {
  onlyIfContainsSeparator = false,
  number = { minimumDigits = 5, groupLength = 3 },
  hexadecimal = { minimumDigits = 0, groupLength = 2 },
  binary = { minimumDigits = 0, groupLength = 4 },
  octal = { minimumDigits = 0, groupLength = 4 },
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 4} */}
```js
const population = 1000000;
const price = 12_34_567;
const color = 0xFF8800;
const flags = 0b10101010;
```

Not reported:

{/* good */}
```js
const population = 1_000_000;
const price = 1_234_567;
const color = 0xFF_88_00;
const flags = 0b1010_1010;
const year = 2026;
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `onlyIfContainsSeparator` | `false` | Only check numbers that already have a `_`. |
| `number` | `{ "minimumDigits": 5, "groupLength": 3 }` | Settings for decimal numbers. |
| `hexadecimal` | `{ "minimumDigits": 0, "groupLength": 2 }` | Settings for `0x` numbers. |
| `binary` | `{ "minimumDigits": 0, "groupLength": 4 }` | Settings for `0b` numbers. |
| `octal` | `{ "minimumDigits": 0, "groupLength": 4 }` | Settings for `0o` numbers. |

Each of the four kinds takes these settings:

| Setting | What it does |
| --- | --- |
| `groupLength` | How many digits are in a group. |
| `minimumDigits` | Numbers with fewer digits have no separators. |
| `onlyIfContainsSeparator` | Overrides the top-level option for this kind of number. |
| `fractionGroupLength` | Only for `number`. How many digits are in a group after the decimal point, counted from the left. By default the fraction has no separators. |

With `onlyIfContainsSeparator`, a number without separators is left alone, and one with separators must have them in the right places:

{/* good {"options": [{"onlyIfContainsSeparator": true}]} */}
```js
const population = 1000000;
```

{/* bad {"options": [{"onlyIfContainsSeparator": true}]} */}
```js
const population = 10_00000;
```

To leave hexadecimal numbers such as colors alone unless they have a separator:

{/* good {"options": [{"hexadecimal": {"onlyIfContainsSeparator": true}}]} */}
```js
const color = 0xFF8800;
```

With `minimumDigits`, shorter numbers must not have separators:

{/* good {"options": [{"number": {"minimumDigits": 7}}]} */}
```js
const timeout = 30000;
const population = 1_000_000;
```

{/* bad {"options": [{"number": {"minimumDigits": 7}}]} */}
```js
const timeout = 30_000;
```

With `fractionGroupLength`:

{/* bad {"options": [{"number": {"fractionGroupLength": 3}}]} */}
```js
const pi = 3.14159265;
```

{/* good {"options": [{"number": {"fractionGroupLength": 3}}]} */}
```js
const pi = 3.141_592_65;
```
