Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby formatting for ordinals: '1' as '1st', '2' as '2nd' etc

Is anything baked into ruby or rails to handle formatting for ordinals: '1' as '1st', '2' as '2nd', etc.?

like image 765
Joel Wickard Avatar asked Jul 22 '10 17:07

Joel Wickard


1 Answers

Looks like you are looking for ordinalize:

The Ruby on Rails framework is chock full of interesting little nuggets. Ordinalize is a number extension that returns the corresponding ordinal number as a string. For instance, 1.ordinalize returns “1st” and 22.ordinalize return “22nd”.

Example:

place = 3 puts "You are currently in #{place.ordinalize} place."

Result:

You are currently in 3rd place.

like image 171
Chris Shaffer Avatar answered Nov 01 '22 02:11

Chris Shaffer