Thursday, March 31, 2016

integer or day suffix (ordinal indicator)

Suffixes as th, st, nd, rd.
(n):
    n = abs(n)
    d21 = n % 100
    switch d21:
        case 11:
        case 12:
        case 13:
            return "th"
    d1 = n % 10
    switch d1:
        case 1:
            return "st"
        case 2:
            return "nd"
        case 3:
            return "rd"
        default:
            return "th"
This is a bit faster.
suffixmap = new map() {
    {11, "th"},
    {12, "th"},
    {13, "th"},
    { 1, "st"},
    { 2, "nd"},
    { 3, "rd"}
    }

(n):
    n = abs(n)
    d21 = n % 100
    if suffixmap.has(d21):
        return suffixmap[d21]
    d1 = n % 10
    if suffixmap.has(d1):
        return suffixmap[d1]
    return "th"

No comments:

Post a Comment