esamattis / underscore.string

String manipulation helpers for javascript
http://esamattis.github.com/underscore.string/
3.37k stars 375 forks source link

Add ordinal suffix #493

Open jwerre opened 8 years ago

jwerre commented 8 years ago
module.exports = function ordinal(num) {
    var suffix=["th","st","nd","rd"],
        val=num%100;
    return num+(suffix[(val-20)%10]||suffix[val]||suffix[0]);
};

_.ordinal(1) // 1st
_.ordinal(2) // 2nd
_.ordinal(3) // 3rd
_.ordinal(4) // 4th