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

Support additional scientific notation styles? #362

Open
sffc opened this issue Jul 11, 2019 · 10 comments
Open

Support additional scientific notation styles? #362

sffc opened this issue Jul 11, 2019 · 10 comments
Labels
c: numbers Component: numbers, currency, units s: comment Status: more info is needed to move forward

Comments

@sffc
Copy link
Contributor

sffc commented Jul 11, 2019

Based on #164, we are adding scientific notation via { notation: "scientific" }. We are using the LDML format for the output, which is

1.23E4

Based on a localizable pattern and exponent separator:

However, this is not necessarily the most "human-friendly" scientific format. Users might expect something like the following:

1.23×10⁴

Users may also prefer to use HTML tags for the superscript. This also helps support non-Latin numbering systems since the superscript digits are only available in Latin.

1.23×10<sup>4</sup>

This could be specified by a new field like scientificDisplay, similar to compactDisplay.

{ notation: "scientific", scientificDisplay: "e" }  // default
{ notation: "scientific", scientificDisplay: "superscriptDigits" }
{ notation: "scientific", scientificDisplay: "html" }
@ray007
Copy link

ray007 commented Jul 12, 2019

And if you have units, SI-prefixes may be the way to go.

@markusicu
Copy link

ICU has this functionality in http://icu-project.org/apiref/icu4c/classicu_1_1ScientificNumberFormatter.html
It does not require extra CLDR data.

@sffc sffc added c: numbers Component: numbers, currency, units s: comment Status: more info is needed to move forward labels Sep 23, 2019
@Yaffle
Copy link

Yaffle commented Jun 28, 2020

Btw, what MathML to use for a number in scientific notation, like 1.23×10⁻⁴ ?
<mn>1.23×10⁻⁴</mn>
or
<mrow><mn>1.23</mn><mo>×</mo><msup><mn>10</mn><mrow><mo>−</mo><mn>4</mn></mrow></msup></mrow>
?

@ryzokuken
Copy link
Member

If we directly expose createMarkupInstance(), users could easily do something like the MathML example by themselves, but I'm not sure if it's too high level for an API like ecma402. Perhaps just an additional scientificDisplay: "mathml" should work better?

@sffc
Copy link
Contributor Author

sffc commented Jul 6, 2020

Another point: you can use formatToParts to get this behavior. Although the spec doesn't guarantee that the exponent always follows a certain pattern following the mantissa, in practice, this is true in every locale.

const nf = new Intl.NumberFormat("es-MX", { notation: "scientific" });
nf.formatToParts(5.55e-4);

/*
[
  {"type":"integer","value":"5"},
  {"type":"decimal","value":"."},
  {"type":"fraction","value":"55"},
  {"type":"exponentSeparator","value":"E"},
  {"type":"exponentMinusSign","value":"-"},
  {"type":"exponentInteger","value":"4"}
]
*/

You can search for "exponentSeparator" and replace it with "×10", and then wrap the "exponentMinusSign" and "exponentInteger" in <sup>.

@ryzokuken
Copy link
Member

@sffc

Another point: you can use formatToParts to get this behavior.

This, on the other hand, seems too low level to me. I suppose atleast html (not quite sure about mathml yet, but I suppose that too) is a common enough use-case to be easier to achieve?

@sffc
Copy link
Contributor Author

sffc commented Jul 6, 2020

This, on the other hand, seems too low level to me. I suppose atleast html (not quite sure about mathml yet, but I suppose that too) is a common enough use-case to be easier to achieve?

I demonstrated that it's possible today for someone to write a small library on top of the existing Intl.NumberFormat that mostly works. That violates point 3 in API surface guidelines I proposed in #442 (comment). That being said, this feature could still move forward, but we should have a compelling case that there are a lot of users who would benefit from this feature, and that it is sufficiently difficult to implement in user land that it belongs in the standard library.

@ryzokuken
Copy link
Member

I hadn't realized that this wasn't a WIP. I don't feel strongly enough about it unless we go ahead and add the others, thanks for the clarification.

@sffc sffc changed the title Support additional scientific notation styles Support additional scientific notation styles? Jul 6, 2020
@anba
Copy link
Contributor

anba commented Jul 6, 2020

formatToParts was added for exactly this purpose, i.e. allowing additional html formatting.

@Yaffle
Copy link

Yaffle commented Jul 9, 2020

Another variant is to extract the exponent from the number and format the coefficient and the exponent separately. Well... The accuracy will be lost for some numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: numbers Component: numbers, currency, units s: comment Status: more info is needed to move forward
Projects
None yet
Development

No branches or pull requests

6 participants