Answers for "ordinal numbers js"

2

javascript add nd st th to number

function ordinal(number) {
    const english_ordinal_rules = new Intl.PluralRules("en", {
        type: "ordinal"
    });
    const suffixes = {
        one: "st",
        two: "nd",
        few: "rd",
        other: "th"
    }
    const suffix = suffixes[english_ordinal_rules.select(number)];
    return (number + suffix);
}

ordinal(3); /* output: 3rd */
ordinal(111); /* output: 111th */
ordinal(-1); /* output: -1st */
Posted by: Guest on June-17-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language