Nerrolken
Nerrolken

Reputation: 2005

Obj-C: Convert INT to text formatted as "sixth" or "twenty-third"

I don't really know what to call this format, maybe "counted text" or something. (Not having a good name for it also makes it hard to search for answers, so if this has been answered before, I apologize for the double-post.)

I want to convert the integer "6" into the string "sixth", or "23" into "twenty-third". I've found answers like this one about converting with the NSNumberFormatterSpellOutStyle, but that gives me "six" or "twenty-three". I suppose I could do that and then write some complex thing to check the last few characters of the string and append "th" or replace "ree" with "ird", but I'm wondering if there is a more elegant solution?

Does anyone know of any built-in methods for this, or any code samples that can get this done?

Upvotes: 1

Views: 113

Answers (1)

Gobot
Gobot

Reputation: 2480

I think what you might be thinking of is ordinal numbers. Try these libraries out:

Also reference this SO post: Objective-C: format numbers to ordinals: 1, 2, 3, .. to 1st, 2nd, 3rd

Upvotes: 1

Related Questions