This browser does not support basic Web standards, preventing the display of our site's intended design. May we suggest that you upgrade your browser?

Web Development Code Library RubyNumbers to ordinals

Numbers to ordinals

number_to_ordinal(12) => "12th" number_to_ordinal(7) => "7th" Note that floats will be converted to ints (without rounding) before processing.

def number_to_ordinal(num)
  num = num.to_i
  if (10...20)===num
    "#{num}th"
  else
    g = %w{ th st nd rd th th th th th th }
    a = num.to_s
    c=a[-1..-1].to_i
    a + g[c]
  end
end
Excellence. Our Measure. Our Motto. Our Goal.