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.
Configure#
Off by default. To turn it on:
[lint.rules]
"unicorn/numeric-separators-style" = "error" # or "warn"Every option, set to its default:
[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 },
}]Examples#
const population = 1000000Invalid group length in numeric value.;
const price = 12_34_567Invalid group length in numeric value.;
const color = 0xFF8800Invalid group length in numeric value.;
const flags = 0b10101010Invalid group length in numeric value.;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:
{ "onlyIfContainsSeparator": true }No problemsconst population = 1000000;{ "onlyIfContainsSeparator": true }1 problemconst population = 10_00000Invalid group length in numeric value.;To leave hexadecimal numbers such as colors alone unless they have a separator:
{ "hexadecimal": { "onlyIfContainsSeparator": true } }No problemsconst color = 0xFF8800;With minimumDigits, shorter numbers must not have separators:
{ "number": { "minimumDigits": 7 } }No problemsconst timeout = 30000;
const population = 1_000_000;{ "number": { "minimumDigits": 7 } }1 problemconst timeout = 30_000Invalid group length in numeric value.;With fractionGroupLength:
{ "number": { "fractionGroupLength": 3 } }1 problemconst pi = 3.14159265Invalid group length in numeric value.;{ "number": { "fractionGroupLength": 3 } }No problemsconst pi = 3.141_592_65;