Is there a way in Objective-C to take a number and write it down?

I’m looking for a way to take a number (say 5 or 207), conjure it and return my ordinal form as a string (five or two hundred seven). All I could find was code that takes a number and returns its ordinal suffix (st, nd, rd, th) and parts of the English grammar guides that say you must specify ordinals.

I am looking for a way to get a numbered number with its ordinal suffix (fifth or two hundred and seventh).

+6
source share
4 answers

Update : today I met this answer , suggesting to use t its library under the MIT license, which also supports several other languages. Hope this helps someone.

Old answer:
I encoded a script that can do this, but only in English:

- (NSString*)getSpelledOutNumber:(NSInteger)num { NSNumber *yourNumber = [NSNumber numberWithInt:(int)num]; NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setNumberStyle:NSNumberFormatterSpellOutStyle]; [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en"]]; return [formatter stringFromNumber:yourNumber]; } - (NSString*)removeLastCharOfString:(NSString*)aString { return [aString substringToIndex:[aString length]-1]; } - (NSString*)getSpelledOutOrdinalNumber:(NSInteger)num { NSString *spelledOutNumber = [self getSpelledOutNumber:num]; // replace all '-' spelledOutNumber = [spelledOutNumber stringByReplacingOccurrencesOfString:@"-" withString:@" "]; NSArray *numberParts = [spelledOutNumber componentsSeparatedByString:@" "]; NSMutableString *output = [NSMutableString string]; NSUInteger numberOfParts = [numberParts count]; for (int i=0; i<numberOfParts; i++) { NSString *numberPart = [numberParts objectAtIndex:i]; if ([numberPart isEqualToString:@"one"]) [output appendString:@"first"]; else if([numberPart isEqualToString:@"two"]) [output appendString:@"second"]; else if([numberPart isEqualToString:@"three"]) [output appendString:@"third"]; else if([numberPart isEqualToString:@"five"]) [output appendString:@"fifth"]; else { NSUInteger characterCount = [numberPart length]; unichar lastChar = [numberPart characterAtIndex:characterCount-1]; if (lastChar == 'y') { // check if it is the last word if (numberOfParts-1 == i) { // it is [output appendString:[NSString stringWithFormat:@"%@ieth ", [self removeLastCharOfString:numberPart]]]; } else { // it isn't [output appendString:[NSString stringWithFormat:@"%@-", numberPart]]; } } else if (lastChar == 't' || lastChar == 'e') { [output appendString:[NSString stringWithFormat:@"%@th-", [self removeLastCharOfString:numberPart]]]; } else { [output appendString:[NSString stringWithFormat:@"%@th ", numberPart]]; } } } // eventually remove last char unichar lastChar = [output characterAtIndex:[output length]-1]; if (lastChar == '-' || lastChar == ' ') return [self removeLastCharOfString:output]; else return output; } 

Usage is quite simple:

 NSString *ordinalNumber = [self getSpelledOutOrdinalNumber:42]; 

The number will be forty seconds. Hope this helps you.

+10
source

This should have been accepted as the correct answer. NSNumberFormatter will do the job, and this is a standard approach, not some shaky workaround.

Here is an example:

 NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle]; NSString* stringFromNumber = [numberFormatter stringFromNumber:your_number_goes_here]; NSLog( @"%@", stringFromNumber ); 

It will output β€œone” for 1 β€œtwo” for 2, etc. In addition, it works great for non-English locales. For example, if you change the formatter locale to German:

 numberFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"DE"]; 

the above code will print: 'eins' for 1 'zwei' for 2, etc.

+4
source

I once wrote a tool called vpi2english. This is part of my vl toolkit written in Matlab. But the code is pretty simple and can be converted to another language if you choose. It essentially takes a (decimal) digital string, breaks it into pieces three digits at a time, and writes them each in words.

 >> vpi2english(vpi('2331546567543686356564321')) ans = two septillion, three hundred thirty one sextillion, five hundred forty six quintillion, five hundred sixty seven quadrillion, five hundred forty three trillion, six hundred eighty six billion, three hundred fifty six million, five hundred sixty four thousand, three hundred twenty one 

It currently works with numbers equal to 1 less than 1e306, which is so large that I can find names for such numbers on the Internet.

 >> vpi2english(999999*vpi(10)^300) ans = nine hundred ninety nine centillion, nine hundred ninety nine novemnonagintillion 
+2
source

Short answer: NO

First of all, this task does not apply to Objective-C as a programming language. Secondly, the result should depend on the user locale.

-6
source

Source: https://habr.com/ru/post/892888/


All Articles