How to convert numbers into indicative string - objective-c

I just want to know how can I can convert the numbers 1, 2 or 3 into First, second or Third ?
Any help would be appreciated.

Try this,
- (NSString *)getOrdinalTextFor:(NSInteger)number {
NSNumber *numberValue = [NSNumber numberWithInteger:number];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterOrdinalStyle];
return [numberFormatter stringFromNumber:numberValue];
}

See this one may be it helps you,
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterSpellOutStyle];
NSString* numberString = [formatter stringFromNumber:[NSNumber numberWithInt: 3]];
NSLog(#"%#",numberString);

enter link description hereDownload Formatter kit from GitHub
TTTOrdinalNumberFormatter *ordinalNumberFormatter = [[TTTOrdinalNumberFormatter alloc] init];
[ordinalNumberFormatter setGrammaticalGender:TTTOrdinalNumberFormatterMaleGender];
NSNumber *number = [NSNumber numberWithInteger:2];
NSString *str = [ordinalNumberFormatter stringFromNumber:number];
NSLog(#"%#", str);
Read up this link, its very helpful: Number Formatting

Related

Conver text in numbers to digits

I have an application for the Apple Watch that uses Siri for getting input from the user. I need to get the response in numbers when the user speak, but the below method only returns numbers as strings, e.g. "five" instead of "5". Any help on how I can achieve this?
presentTextInputControllerWithSuggestions
NSNumberFormatter can convert from text to numbers.
Example
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterSpellOutStyle;
NSLog(#"%#", [formatter numberFromString:#"thirty-four"]);
NSLog(#"%#", [formatter numberFromString:#"three point five"]);
formatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:[NSLocale localeIdentifierFromComponents:#{NSLocaleLanguageCode: #"es"}]];
NSLog(#"%#", [formatter numberFromString:#"ocho"]);
Try this.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterSpellOutStyle;
NSNumber *num = [formatter numberFromString:#"yourWord"];
int value = [num intValue];

How to avoid grouping separator in NSNumberFormater until 5 digits

I have an application that needs to display metric units in millimeters and based on the research that I have done (e.g. http://physics.nist.gov/cuu/pdf/sp811.pdf), it is recommended that you don't add grouping separators until you have 5 digits. Is this possible with NSNumberFormatter? I know I could test the value and enable/disable grouping based on its size but I would rather use the appropriate configuration for the NSNumberFormatter if it exists. (BTW, I am on Mac OS X) So for example:
3456 should be shown as 3456 mm
10234 should be shown as 10,234 mm (assuming comma is the grouping separator)
I have read the Apple docs and didn't see anything. I experimented with different searches to find the answer on Stackoverflow but couldn't find a similar question.
Here is how I am currently setting up my NSNumberFormatter:
if (numberFormatter == nil) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
[numberFormatter setMinimumFractionDigits:0];
[numberFormatter setMaximumFractionDigits:2];
}
NSNumber *numberInMM = [NSNumber numberWithFloat:fLenInMMs];
NSString *numberStr = [numberFormatter stringFromNumber:numberInMM];
numberStr = [numberStr stringByAppendingString:#" mm"];
long long d = fLenInMMs;
if (d>9999) {
NSNumberFormatter *numberFormatter=nil;
if (numberFormatter == nil) {
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setLocale:[NSLocale currentLocale]];
[numberFormatter setMinimumFractionDigits:0];
[numberFormatter setMaximumFractionDigits:2];
}
NSNumber *numberInMM = [NSNumber numberWithFloat:fLenInMMs];
NSString *numberStr = [numberFormatter stringFromNumber:numberInMM];
numberStr = [numberStr stringByAppendingString:#" mm"];
NSLog(#"%#", numberStr);
}
else {
NSNumber *numberInMM = [NSNumber numberWithFloat:fLenInMMs];
NSString *numberStr = [NSString stringWithFormat:#"%# mm",numberInMM];
}
EDIT
You can also subclass NSNumberFormatter and implement
- (NSString *)stringFromNumber:(NSNumber *)number
{
NSNumber *nm = [NSNumber numberWithInt:9999];
if (NSOrderedAscending==[number compare:nm]) {
return [NSString stringWithFormat:#"%# mm",number];
}
return [super stringFromNumber:number];
}

Sorting NSMutablearray by NSString as a date

I have an array, which is filled out with string objects. Inside each object is the name of the object and the string, seperated by " - ", ex. "Object1 - 26.05.2012 ". I would like to sort my array by the date in the string, and not the name, descending Is this possible?
As #Vladimir pointed out, it would be much better to separate the name and the string from each other and sort by the date.
NSMutableArray *newArray = [[NSMutableArray alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd.MM.yyyy"];
for (NSString *str in yourArray)
{
NSRange rangeForDash = [str rangeOfString:#"-"];
NSString *objectStr = [str substringToIndex:rangeForDash.location];
NSString *dateStr = [str substringFromIndex:rangeForDash.location+1];
NSDate *date = [formatter dateFromString:dateStr];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:objectStr, #"object", date, #"date", nil];
[newArray addObject:dic];
}
NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:#"date" ascending:NO];
[newArray sortUsingDescriptors:[NSArray arrayWithObjects:sortDesc, nil]];
[sortDesc release];
[formatter release];
newArray will be sorted but will have dictionary objects, each containing 2 strings.
Quick and dirty fix - rearrange the string so that the date is first
str = [[str substringFromIndex:someIndex] stringByAppendingString:[str substringToIndex:someIndex]];
then just switch everything back once the array is sorted.

How to use NSNumberFormatter to display currency?

I want to format a number with NSNumberFormatter to be displayed as currency like this:
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
NSNumber *myNumber = [NSNumber numberWithDouble:[#"0.058" doubleValue]];
[numFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
This gives me 0,06 €. But I want it to be 5,8 ct. Any ideas?
You can configure NSNumberFormatter like that :
NSNumberFormatter *numFormatter = [[NSNumberFormatter alloc] init];
[numFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numFormatter setCurrencySymbol:#"ct"];
[numFormatter setMultiplier:100];
NSNumber *myNumber = [NSNumber numberWithDouble:[#"0.058" doubleValue]];

How to truncate .000 zeros from double with NSNumberFormatter

Ok so if I have a double to begin with how do I limit it or truncate digits to 1dp since I know they're not needed to be viewed
double a = 1.6;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:a]];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGeneratesDecimalNumbers:YES];
[numberFormatter setMaximumFractionDigits:1];
[numberFormatter setMinimumFractionDigits:1];
[numberFormatter setAlwaysShowsDecimalSeparator:YES];
NSLog(#"a raw: %f", a); //outputs 1.600000
myLabel.text = [NSString stringWithFormat:#"%#",numberAsString];
NSLog(#"Formatted a: %#", numberAsString); //outputs 2
[numberFormatter release];
So the problem is it rounds the value and leaves no decimal place, I'm trying to get it to output just 1.6 for myLabel.
I am not sure but you can try with NSString *formatted = [NSString stringWithFormat:#"%.1f", a];, then use the formatted as the label text