sindresorhus/eslint-plugin-unicorn

Change `numeric-separators-style` after decimal point

Closed

#2,283 opened on Feb 19, 2024

 (2 comments) (3 reactions) (0 assignees)JavaScript (468 forks)user submission
enhancementhelp wanted

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

Currently, unicorn/numeric-separators-style reports an error for this code:

// hot code path, precalculate 4 (sqrt(2) - 1) / 3
const kappa = 0.55228474983;
//            ^^^^^^^^^^^^^
//            Invalid group length in numeric value.eslint
//            [unicorn/numeric-separators-style](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v49.0.0/docs/rules/numeric-separators-style.md)

Current behavior

With the default config, this is how the rule expects the number to be formatted

In my personal opinion, the decimal point is "lost" amongst the underscores

const kappa = 0.552_284_749_83; // not very readable

I could set a groupLength of 5...

const kappa = 0.55228_47498_3; // Wikipedia style - readable

...but this gets in the way of formatting large numbers

const one_million = 10_00000; // uh-oh, very misleading

Improvement

This can be resolved by having a different groupLength before and after the decimal place. For example, Wikipedia formats their numbers like this:

149 597 870 700 metres is 1 astronomical unit 3.14159 26535 89793 23846 is π rounded to 20 decimal places 2.71828 18284 59045 23536 is e rounded to 20 decimal places.

https://en.wikipedia.org/wiki/Decimal_separator#Data_versus_mask

{
	number: {
		minimumDigits: 5,
		groupLength: 3,
		fractionGroupLength: 5 // proposed addition, defaults to be same as groupLength
	}
};
const kappa = 0.55228_47498_3; 

const one_million = 1_000_000;

Alternatively, it would be fine to disable checking of numeric-separators-style after the decimal point

const kappa = 0.55228474983; // totally acceptable

const one_million = 1_000_000;

Contributor guide