Nodejs Number to Ordinal Format toOrdinal()

Here you can find the source of toOrdinal()

Method Source Code

/**/*  w  w w .j  a v  a2s  .c om*/
 * Converts a number to string ordinal number.
 * @see https://gist.github.com/jlbruno/1535691
 * @returns {string}
 */
Number.prototype.toOrdinal = function() {
    var n,
        s = ["th","st","nd","rd"],
        v = n % 100;
    return n + (s[(v - 20) % 10] || s[v] || s[0]);
};

Related

  1. toOrdinal()
    Number.prototype.toOrdinal = function() {
      var n = this % 100;
      var suffix = ["th", "st", "nd", "rd", "th"];
      var ord = n < 21 ? (n < 4 ? suffix[n] : suffix[0]) : (n % 10 > 4 ? suffix[0] : suffix[n % 10]);
      return this + ord;
    };
    
  2. toOrdinal()
    Number.prototype.toOrdinal = function() {
      var s = ['th','st','nd','rd'];
      var v = this%100;
      return this + (s[(v-20)%10]||s[v]||s[0]);
    };
    
  3. toOrdinal()
    Number.prototype.toOrdinal = function() {
        var n = this % 100,
            suffix = ['th', 'st', 'nd', 'rd', 'th'],
            ord = n < 21 ? (n < 4 ? suffix[n] : suffix[0]) : (n % 10 > 4 ? suffix[0] : suffix[n % 10]);
        return this + ord;
    };