Converting Numbers to Ordinals - Node.js Number

Node.js examples for Number:Format

Description

Converting Numbers to Ordinals

Demo Code

function getOrdinal(n) {
  var ord = "th";
  if (n % 10 == 1 && n % 100 != 11) {
    ord = "st";/*from w ww . ja  v a2s .  c o  m*/
  } else if(n % 10 == 2 && n % 100 != 12) {
    ord = "nd";
  } else if(n % 10 == 3 && n % 100 != 13) {
    ord = "rd";
  }
  return ord;
}

Related Tutorials