Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change numeric-separators-style after decimal point #2283

Open
oscarhermoso opened this issue Feb 19, 2024 · 2 comments
Open

Change numeric-separators-style after decimal point #2283

oscarhermoso opened this issue Feb 19, 2024 · 2 comments

Comments

@oscarhermoso
Copy link

oscarhermoso commented Feb 19, 2024

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;
@sindresorhus
Copy link
Owner

I agree. I also see little value from forcing separators for the fractional part. I would go with fractionGroupLength and default it to Infinity (alternatively a separate option to not enforce this for the fraction part).

@fregante
Copy link
Collaborator

precalculate 4 (sqrt(2) - 1) / 3

Is this piece of code run on Node or in the browser? I think some/most minifiers will attempt to resolve simple static calculations, simply to save bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants