Skip to main content

Converts a number into its respective ordinal representation.

/**
 * Converts a number into its respective ordinal representation
 *
 * Example:
 *    1 => 1ˢᵗ
 *    3 => 3ʳᵈ
 *
 * @param {Number} no The number to convert.
 * @return {String} The ordinal representation.
 */
function ordinalNumber(no) {
    var s = ['ᵗʰ', 'ˢᵗ', 'ⁿᵈ', 'ʳᵈ'],
        v = no % 100;
    return no + (s[(v - 20) % 10] || s[v] || s[0]);
}