This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Locales and Ordinal values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Locales and Ordinal values" Watch "Locales and Ordinal values" New topic

Locales and Ordinal values

Alan Davey

Joined: Aug 03, 2006
Posts: 3

Are there any methods built-in to java where I can pass in an integer value and get an ordinal value (i.e. 1st, 2nd, 3rd, etc.) in return? I'm looking for something that will work with the current locale, much like number formats will return either a decimal or comma seperators for numbers based on the current locale.

I've searched here and on google, but so far I haven't had much luck finding what I want.

Any help would be greatly appreciated.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15042

There is no built-in method in the standard API for this, so you would have to write something yourself, or find it somewhere else.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Aaron Harp

Joined: Jul 12, 2006
Posts: 8
Here is a method I wrote to take an integer and return its ordinal equivalent. Hope it helps.

Aaron Harp

Joined: Jul 12, 2006
Posts: 8
It doesn't take locales into account but that wouldn't be too difficult.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45338
. . . That is a good way to convert cardinal numbers to ordinal, but what about 11th 12th and 13th? CR
Aaron Harp

Joined: Jul 12, 2006
Posts: 8
Oops, didn't think about that.

I agree. Here's the link:
subject: Locales and Ordinal values
It's not a secret anymore!