Skip to content

Instantly share code, notes, and snippets.

@ajmas
Created September 2, 2020 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajmas/e74e4b46effcadca8a33ff87c798a739 to your computer and use it in GitHub Desktop.
Save ajmas/e74e4b46effcadca8a33ff87c798a739 to your computer and use it in GitHub Desktop.
International Number Formatting in JS
const suffixes = {
en: {
one: 'st',
two: 'nd',
few: 'rd',
other: 'th'
},
fr: {
one: 'er',
two: 'e',
few: 'e',
other: 'e'
},
de: {
one: 'te',
two: 'te',
few: 'te',
other: 'te'
}
};
function getNumberWithOrdinal(number, language = 'en') {
language = language.substring(0, 2);
const pluralRules = new Intl.PluralRules(language, { type: 'ordinal' });
return `${number}${suffixes[language][pluralRules.select(number)]}`;
}
export { getNumberWithOrdinal };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment