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

Add style ordinal/cardinal to NumberFormat (RBNF) #494

Open
romulocintra opened this issue Aug 10, 2020 · 4 comments
Open

Add style ordinal/cardinal to NumberFormat (RBNF) #494

romulocintra opened this issue Aug 10, 2020 · 4 comments
Labels
c: numbers Component: numbers, currency, units Proposal Larger change requiring a proposal s: comment Status: more info is needed to move forward

Comments

@romulocintra
Copy link
Member

Support formatting ordinals & cardinals

Style Ordinal

console.log(new Intl.NumberFormat('en', { style: 'ordinal' }).format(2));
// 2nd

console.log(new Intl.NumberFormat('pt', { style: 'ordinal' }).format(2));
// 2.º or 2.ª (M/F)

Style Cardinal

console.log(new Intl.NumberFormat('en', { style: 'cardinal' }).format(2));
// second

console.log(new Intl.NumberFormat('pt', { style: 'cardinal' }).format(2));
// segundo or segunda (M/F)
@sffc sffc changed the title Add style ordinal/cardinal to NumberFormat Add style ordinal/cardinal to NumberFormat (RBNF) Aug 10, 2020
@sffc
Copy link
Contributor

sffc commented Aug 10, 2020

Previous issue: #95. Let's keep this issue focused on Spellout and Ordinal specifically. However, the underlying code for these two styles is the same as for the algorithmic numbering systems (native, traditio, finance) listed in #95.

@sffc sffc added c: numbers Component: numbers, currency, units Proposal Larger change requiring a proposal s: comment Status: more info is needed to move forward labels Aug 10, 2020
@sffc
Copy link
Contributor

sffc commented Jan 14, 2021

See some good discussion on tc39/proposal-intl-numberformat-v3#22. CC @Bx1

@karlhorky
Copy link

karlhorky commented Jul 25, 2024

Oh this looks great! 😍

new Intl.NumberFormat('en', { style: 'ordinal' }).format(2) // '2nd'

Having a built-in web platform way to create the localized strings "1st", "2nd", "3rd" would be amazing.

Currently, for English ordinals, using either 1) self-written code with Intl.PluralRules() or 2) the ordinal npm package:

// Option 1: self-written w. Intl.PluralRules() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules/PluralRules
const pr = new Intl.PluralRules("en-US", { type: "ordinal" });

const suffixes = new Map([
  ["one", "st"],
  ["two", "nd"],
  ["few", "rd"],
  ["other", "th"],
]);
const formatOrdinals = (n) => {
  const rule = pr.select(n);
  const suffix = suffixes.get(rule);
  return `${n}${suffix}`;
};

formatOrdinals(0); // '0th'
formatOrdinals(1); // '1st'
formatOrdinals(2); // '2nd'
formatOrdinals(3); // '3rd'
// Option 2: ordinal package https://www.npmjs.com/package/ordinal
import ordinal from 'ordinal';

ordinal(1) // '1st'
ordinal(2) // '2nd'
ordinal(3) // '3rd'

I saw that it was accepted by the MessageFormat Working Group in Feb 2024 (but unsure how exactly that interacts with ECMA 402 / any changes to Intl.NumberFormat() 🤔)

I understand that there is still ICU4X / 402 work to be done.

@sffc Maybe for visibility a new issue could be opened in https://github.com/tc39/proposal-intl-numberformat-v3 ? The last one is closed. Or does this need a new Intl.NumberFormat v4 proposal repo?

@eemeli
Copy link
Member

eemeli commented Jul 25, 2024

I saw that it was accepted by the MessageFormat Working Group in Feb 2024 (but unsure how exactly that interacts with ECMA 402 / any changes to Intl.NumberFormat() 🤔)

The MF2 :number option that we've ended up with is select, which accepts the values plural (default), ordinal, and exact.

For style, the only current supported values are decimal (default) and percent.

In other words, supporting MF2 does not require adding ordinal number formatting support to Intl.NumberFormat.

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 Proposal Larger change requiring a proposal s: comment Status: more info is needed to move forward
Projects
None yet
Development

No branches or pull requests

4 participants