I'm looking for an algorithm or function to convert integer number 0,1,2 to Zero,One,Two respectively. How can we do this in Objective-C ?
Asked
Active
Viewed 177 times
0
-
1Single digits only? Localized or english only? – rounak Jul 10 '15 at 12:55
-
1http://stackoverflow.com/questions/3911966/how-to-convert-number-to-words-in-java. Here is an example of such code in java – AdamM Jul 10 '15 at 12:58
-
@AdamM thanks buddy. I'll convert the code – Rahul Vyas Jul 10 '15 at 12:59
-
[Here it is in Swift](http://codereview.stackexchange.com/questions/58878/int-extension-for-translating-integer-to-plain-english) which is probably a better starting point for doing an Objective-C translation. And [here is an Objective-C attempt](http://codereview.stackexchange.com/q/59546/36366). – nhgrif Jul 10 '15 at 13:00
-
@rounak adamM commented right answer. – Rahul Vyas Jul 10 '15 at 13:02
-
@Ashwin thanks I had in mind NSNumberformatter. Thanks – Rahul Vyas Jul 10 '15 at 13:03
2 Answers
4
Apple has a lot of handy formatting functionality built in for many data types. Called a "formatter," they can convert objects to/from string representations.
For your case, you will be using NSNumberFormatter, but if you have an integer you need to convert it to an NSNumber first. See below example.
NSInteger anInt = 11242043;
NSString *wordNumber;
//convert to words
NSNumber *numberValue = [NSNumber numberWithInt:anInt]; //needs to be NSNumber!
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
wordNumber = [numberFormatter stringFromNumber:numberValue];
NSLog(@"Answer: %@", wordNumber);
// Answer: eleven million two hundred forty-two thousand forty-three

Ashwin P
- 501
- 1
- 3
- 19
1
This is my code for 0 to 100 (You can update as per your requirement). WORKING PERFECTLY !!
-(NSDictionary *)algorithm
{
NSArray *myArray = @[@"Zero",@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Ten",@"Eleven",@"Twelve",@"Thirteen",@"Fourteen",@"Fifteen",@"Sixteen",@"Sevteen",@"Eighteen",@"Nineteen"];
NSArray *tensArray = @[@"Twenty",@"Thirty",@"Fourty",@"Fifty",@"Sixty"
,@"Seventy",@"Eighty",@"Ninety",@"One Hundred"];
NSMutableDictionary *numberStringDictionary = [[NSMutableDictionary alloc] init];
NSMutableArray *numberStringsArray = [[NSMutableArray alloc] init];
for(int i=0;i<=100;i++)
{
if(i<20)
{
[numberStringDictionary setObject:myArray[i] forKey:[NSString stringWithFormat:@"%d",i]];
[numberStringsArray addObject:myArray[i]];
NSLog(@"\n%@",myArray[i]);
}
else if(i%10==0)
{
[numberStringDictionary setObject:tensArray[i/10-2] forKey:[NSString stringWithFormat:@"%d",i]];
[numberStringsArray addObject:tensArray[i/10-2]];
NSLog(@"\n%@",tensArray[i/10-2]);
}
else
{
[numberStringDictionary setObject:[NSString stringWithFormat:@"%@ %@",tensArray[i/10-2],myArray[i%10]] forKey:[NSString stringWithFormat:@"%d",i]];
[numberStringsArray addObject:[NSString stringWithFormat:@"%@ %@",tensArray[i/10-2],myArray[i%10]]];
NSLog(@"%@",[NSString stringWithFormat:@"%@ %@",tensArray[i/10-2],myArray[i%10]]);
}
}
return numberStringDictionary;
}

Kumar
- 1,882
- 2
- 27
- 44