Created
September 2, 2020 17:01
-
-
Save ajmas/e74e4b46effcadca8a33ff87c798a739 to your computer and use it in GitHub Desktop.
International Number Formatting in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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