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

signDisplay: missing option for a reasonable use-case #17

Closed
pspraveenkr opened this issue Jul 9, 2020 · 5 comments
Closed

signDisplay: missing option for a reasonable use-case #17

pspraveenkr opened this issue Jul 9, 2020 · 5 comments
Labels
has-consensus Has consensus and ready to implement

Comments

@pspraveenkr
Copy link

Use-case:
I'd like to round negative values but I don't want -0 to be formatted into a string with "minus" sign i.e. I prefer "0" instead of "-0"

For example:

const formatter = new Intl.NumberFormat("en", {
  maximumFractionDigits: 1
});

console.log(formatter.format(-0.03));
// Returns: -0
// Desired result: 0

The proposed spec for signDisplay supports these options: auto, always, never and exceptZero. But, none of them seem to produce the output desired for the use-case above - without compromising on something else.

exceptZero comes close, but in addition to removing the minus sign from -0, it also adds a plus sign to positive numbers (as expected) which is not desired for the use-case above.

var formatter = new Intl.NumberFormat("en", {
  maximumFractionDigits: 1,
  signDisplay: "exceptZero"
});

console.log(formatter.format(-0.03));
// Returns the desired result: 0  👍 
// but...

console.log(formatter.format(123));
// Returns: +123 👎 
// Desired result: 123

I found this table containing examples of signDisplay options here: https://github.com/tc39/proposal-unified-intl-numberformat/blob/master/README.md#iii-sign-display. I was hoping to find an option that would produce -1 | 0 | 0 | 1 | NaN, but such an option does not seem to exist.

Am I missing something? What is the recommendation for handling the use-case above?
Thank you.

@sffc
Copy link
Collaborator

sffc commented Jul 10, 2020

Thanks for the feedback!

Maybe we could call this "onlyNegative"?

In the mean time, you can do

class Formatter {
  constructor(locale) {
    this.negative = new Intl.NumberFormat(locale, { signDisplay: "exceptZero" });
    this.positive = new Intl.NumberFormat(locale, { signDisplay: "auto" });
  }
  format(input) {
    if (input < 0) return this.negative.format(input);
    else return this.positive.format(input);
  }
}

@pspraveenkr
Copy link
Author

Maybe we could call this "onlyNegative"?

Yes, either "onlyNegative" or just "negative" would be suitable.

Thanks for the interim solution!

@sffc sffc added the discuss Needs discussion to make progress label Oct 8, 2020
@sffc
Copy link
Collaborator

sffc commented Feb 4, 2021

@sffc
Copy link
Collaborator

sffc commented Feb 11, 2021

2021-02-11: We will add this to the proposal as signDisplay: "negative".

@sffc sffc added has-consensus Has consensus and ready to implement and removed discuss Needs discussion to make progress labels Feb 11, 2021
@sffc
Copy link
Collaborator

sffc commented Apr 18, 2021

This is added to the README as of #28. It will be added to the spec draft in #29, which will then unblock implementations. This issue will be closed automatically when #28 is merged to reflect that it is part of the proposal.

@sffc sffc closed this as completed in 8d8e9a6 Apr 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-consensus Has consensus and ready to implement
Projects
None yet
Development

No branches or pull requests

2 participants