[Proposal] Compact Decimal Format to abbreviate large numbers
caridy opened this issue · comments
CLDR contains the details to abbreviate large numbers, e.g.:
"decimalFormats-numberSystem-latn": {
"standard": "#,##0.###",
"long": {
"decimalFormat": {
"1000-count-one": "0 thousand",
"1000-count-other": "0 thousand",
"10000-count-one": "00 thousand",
"10000-count-other": "00 thousand",
"100000-count-one": "000 thousand",
"100000-count-other": "000 thousand",
"1000000-count-one": "0 million",
"1000000-count-other": "0 million",
"10000000-count-one": "00 million",
"10000000-count-other": "00 million",
"100000000-count-one": "000 million",
"100000000-count-other": "000 million",
"1000000000-count-one": "0 billion",
"1000000000-count-other": "0 billion",
"10000000000-count-one": "00 billion",
"10000000000-count-other": "00 billion",
"100000000000-count-one": "000 billion",
"100000000000-count-other": "000 billion",
"1000000000000-count-one": "0 trillion",
"1000000000000-count-other": "0 trillion",
"10000000000000-count-one": "00 trillion",
"10000000000000-count-other": "00 trillion",
"100000000000000-count-one": "000 trillion",
"100000000000000-count-other": "000 trillion"
}
},
"short": {
"decimalFormat": {
"1000-count-one": "0K",
"1000-count-other": "0K",
"10000-count-one": "00K",
"10000-count-other": "00K",
"100000-count-one": "000K",
"100000-count-other": "000K",
"1000000-count-one": "0M",
"1000000-count-other": "0M",
"10000000-count-one": "00M",
"10000000-count-other": "00M",
"100000000-count-one": "000M",
"100000000-count-other": "000M",
"1000000000-count-one": "0B",
"1000000000-count-other": "0B",
"10000000000-count-one": "00B",
"10000000000-count-other": "00B",
"100000000000-count-one": "000B",
"100000000000-count-other": "000B",
"1000000000000-count-one": "0T",
"1000000000000-count-other": "0T",
"10000000000000-count-one": "00T",
"10000000000000-count-other": "00T",
"100000000000000-count-one": "000T",
"100000000000000-count-other": "000T"
}
}
},
@rxaviers proposed this feature a while ago IIRC, but I could find the thread, so I'm posting it here to formalize the proposal.
Proposal
This proposal goes hand-to-hand with the pluralization (#34) since it will have to compute what's the pluralization token to choose the right format (In the CLDR data you will see that for english we have a lot of *-count-one
and *-count-other
).
The initial proposal could be to add one more configuration to specify either:
a) best-fit
for the abbreviation, which means we choose the biggest matching decimal from the segments
b) the decimal reference to force to use a particular formatting option (e.g.: 10000
, which could produce 1234K
);
new Intl.NumberFormat('en', { compact: 'best-fit' }).format(1234000); // 1.2M
new Intl.NumberFormat('en', { compact: '4-digits' }).format(1234000); // 1234K
Open questions
- how to match this with currency values so we can produce something like
$12M
. - the name of the configuration option when creating a
new Intl.NumberFormat()
1M
vs1.2M
1M
vs1 Million
(this will probably require another configuration to specifylong
vsshort
.- rounding and custom rounding settings? (related to rxaviers/ecma402-number-format-round-option#1 (comment))
More details about CLDR Compact Decimal Format here: http://www.unicode.org/cldr/charts/27/summary/pl.html#4032
Probably same algorithm will be useful as for issue #32. ("1.2 M" vs. "2.5 KB")
Spec: UTS #35 (HEAD)
cc @steveluscher due to its related proposal https://gist.github.com/steveluscher/4b54e08aa1e0536b7cb9
Can you link to this rounding discussion in your list of open questions, @caridy?
@steveluscher added.
Any progress/plan for this proposal?
This is quite useful for some statistical data shown in analysis softwares for localization.
It is an active proposal, but we need a champion!
See my follow-up in #215
Here is a library being used in applications across the Ember community based on this proposal.
JS lib: https://github.com/snewcomer/cldr-compact-number
Ember addon: https://github.com/snewcomer/ember-short-number
The new notation
option provides this setting when "compact"
is used:
js> print(new Intl.NumberFormat('en', { notation: "compact", compactDisplay: "long" }).format(1_000_000))
1 million
Yep, the proposal has been merged. Closing as Fixed