useNumericSeparators
Summary
Section titled “Summary”- Rule available since:
v2.0.0 - Diagnostic Category:
lint/style/useNumericSeparators - This rule isn’t recommended, so you need to enable it.
- This rule has a safe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/numeric-separators-style - Same as
unreadable_literal
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "style": { "useNumericSeparators": "error" } } }}Description
Section titled “Description”Enforce the use of numeric separators in numeric literals.
Enforces a convention of grouping digits using numeric separators.
Long numbers can become difficult to read, so separating groups of digits with an underscore (_) improves code clarity. This rule also enforces proper usage of the numeric separator, by checking if the groups of digits are of the correct size.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”var a = 1234567890;code-block.js:1:9 lint/style/useNumericSeparators FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Long numeric literal lacks separators.
> 1 │ var a = 1234567890;
│ ^^^^^^^^^^
2 │
ℹ Adding separators helps improve readability and clarity for long numbers.
ℹ Safe fix: Add numeric separators.
1 │ - var·a·=·1234567890;
1 │ + var·a·=·1_234_567_890;
2 2 │
var a = -999_99;code-block.js:1:10 lint/style/useNumericSeparators FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Inconsistent grouping of digits in numeric literal.
> 1 │ var a = -999_99;
│ ^^^^^^
2 │
ℹ Numbers with inconsistently placed separators can be misleading or confusing.
ℹ Safe fix: Use consistent numeric separator grouping.
1 │ - var·a·=·-999_99;
1 │ + var·a·=·-99_999;
2 2 │
var a = 0.1234567;code-block.js:1:9 lint/style/useNumericSeparators FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Long numeric literal lacks separators.
> 1 │ var a = 0.1234567;
│ ^^^^^^^^^
2 │
ℹ Adding separators helps improve readability and clarity for long numbers.
ℹ Safe fix: Add numeric separators.
1 │ - var·a·=·0.1234567;
1 │ + var·a·=·0.123_456_7;
2 2 │
var a = 0b11001100;code-block.js:1:9 lint/style/useNumericSeparators FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Long numeric literal lacks separators.
> 1 │ var a = 0b11001100;
│ ^^^^^^^^^^
2 │
ℹ Adding separators helps improve readability and clarity for long numbers.
ℹ Safe fix: Add numeric separators.
1 │ - var·a·=·0b11001100;
1 │ + var·a·=·0b1100_1100;
2 2 │
var a = 1_234_567_890;var a = -99_999;var a = 0.123_456_7;var a = 0b1100_1100;Options
Section titled “Options”Each numeric literal type binary, octal, decimal, and hexadecimal can be configured with its own options object. Each type accepts minimumDigits or groupLength.
{ "linter": { "rules": { "style": { "useNumericSeparators": { "options": { "decimal": { "minimumDigits": 7, "groupLength": 3 }, "hexadecimal": { "minimumDigits": 4, "groupLength": 2 } } } } } }}<numeric literal type>.minimumDigits
Section titled “<numeric literal type>.minimumDigits”Minimum number of digits required before adding separators. For example, with minimumDigits set to 5, the number 1234 would not require separators, but 12345 would be expected to be formatted as 12_345.
Default values for minimumDigits vary by numeric literal type:
binary:0octal:0decimal:5hexadecimal:0
Invalid
Section titled “Invalid”{ "linter": { "rules": { "style": { "useNumericSeparators": { "options": { "decimal": { "minimumDigits": 8 } } } } } }}12345678;code-block.js:1:1 lint/style/useNumericSeparators FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Long numeric literal lacks separators.
> 1 │ 12345678;
│ ^^^^^^^^
2 │
ℹ Adding separators helps improve readability and clarity for long numbers.
ℹ Safe fix: Add numeric separators.
1 │ - 12345678;
1 │ + 12_345_678;
2 2 │
{ "linter": { "rules": { "style": { "useNumericSeparators": { "options": { "decimal": { "minimumDigits": 8 } } } } } }}1234;12_345_678;<numeric literal type>.groupLength
Section titled “<numeric literal type>.groupLength”Number of digits between separators. For example, with groupLength set to 2, the number 12345 would be expected to be formatted as 1_23_45.
Default values for groupLength vary by numeric literal type:
binary:4octal:4decimal:3hexadecimal:2
Invalid
Section titled “Invalid”{ "linter": { "rules": { "style": { "useNumericSeparators": { "options": { "decimal": { "groupLength": 5 } } } } } }}123_451_2345;code-block.js:1:1 lint/style/useNumericSeparators FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Inconsistent grouping of digits in numeric literal.
> 1 │ 123_451_2345;
│ ^^^^^^^^^^^^
2 │
ℹ Numbers with inconsistently placed separators can be misleading or confusing.
ℹ Safe fix: Use consistent numeric separator grouping.
1 │ - 123_451_2345;
1 │ + 12345_12345;
2 2 │
{ "linter": { "rules": { "style": { "useNumericSeparators": { "options": { "decimal": { "groupLength": 5 } } } } } }}12345_12345;Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.