## NSNumberFormatter and 'th' 'st' 'nd' 'rd' (ordinal) number endings

By : jan
Source: Stackoverflow.com
Question!

Is there a way to use NSNumberFormatter to get the 'th' 'st' 'nd' 'rd' number endings?

EDIT:

Looks like it does not exist. Here's what I'm using.

``````+(NSString*)ordinalNumberFormat:(NSInteger)num{
NSString *ending;

int ones = num % 10;
int tens = floor(num / 10);
tens = tens % 10;
if(tens == 1){
ending = @"th";
}else {
switch (ones) {
case 1:
ending = @"st";
break;
case 2:
ending = @"nd";
break;
case 3:
ending = @"rd";
break;
default:
ending = @"th";
break;
}
}
return [NSString stringWithFormat:@"%d%@", num, ending];
}
``````

By : jan

Here's a Swift solution that cycles through the user's preferred languages until it finds one with known rules (which are pretty easy to add) for ordinal numbers:

``````extension Int {
var localizedOrdinal: String {

func ordinalSuffix(int: Int) -``````
``` By : Alnair ```
``` Many of the solutions here don't handle higher numbers like 112. Here is a simple way to do it. for(int i=0;i By : garafajon Here's a short Int extension for the English language that also accounts for and displays negative integers correctly: extension Int { func ordinal() - By : Luke Chase This video can help you solving your question :) By: admin ```
``` Related Questions How to programmatically send SMS on the iPhone? NSDateFormatter, am I doing something wrong or is this a bug? What are best practices that you use when writing Objective-C and Cocoa? [closed] How can I disable the UITableView selection highlighting? (adsbygoogle = window.adsbygoogle || []).push({}); Multiple NSURLConnection delegates in Objective-C How do I move the UIView to the “bottom” so that the next UIView displays? UIWebView Expose Objective C to JavaScript How to “simplify” UIImagePickerController iPhone OS Utility App - Flipside View and Main View communication What's the best way to find the user's Documents directory on an iPhone? Using default tab-controller created project… first view controller doesn't respond to addsubview iPhone - Memory Leak - NSData dataWithContentsOfUrl & UIWebView How can I create a zip file by using Objective C? Sync Services Client and Schema Xcode Project Structure How to integrate NSURLConnection with UIProgressView? Loading Data Files on iPhone? EXC_BAD_ACCESS signal received Subclassing a UIViewController, when and when not to Add image to a navigationItem's title Child controller updating a label for a cell in parent table view ```
``` ```
``` About Us    Contact Us    Legal    feedback    Copyright © 2015 - All Rights Reserved - www.4answered.com ```