Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert numbers into text?

I have been learning Objective-C with the Kochan book and I can't figure out how to do this exercise program. Only odd numbered exercises are listed online and this one is even. The exercise is to convert numbers into words. So, if "932" was entered, the program should return: "nine three two"

I used a do, while loop but the words came out backwards, as in "two three nine". Can anyone suggest a technique that works for this?

int number, digit;


NSLog(@"Type in your integer.");
scanf("%i", &number);


 do
 {
     digit = number % 10;

     if (digit == 0)
         NSLog(@"zero");
     if (digit == 1)
         NSLog(@"one");
     if (digit == 2)
         NSLog(@"two");
     if (digit == 3)
         NSLog(@"three");
     if (digit == 4)
         NSLog(@"four");
     if (digit == 5)
         NSLog(@"five");
     if (digit == 6)
         NSLog(@"six");
     if (digit == 7)
         NSLog(@"seven");
     if (digit == 8)
         NSLog(@"eight");
     if (digit == 9)
         NSLog(@"nine");

     number /= 10;
 }
while (number != 0);
like image 297
Nadeem Avatar asked Jul 01 '11 20:07

Nadeem


People also ask

How do you convert numerical numbers into words?

Use the SpellNumber function in individual cells Type the formula =SpellNumber(A1) into the cell where you want to display a written number, where A1 is the cell containing the number you want to convert. You can also manually type the value like =SpellNumber(22.50). Press Enter to confirm the formula.

How do I convert all numbers stored as text?

Use Paste Special and Multiply Select the cells that have numbers stored as text. On the Home tab, click Paste > Paste Special. Click Multiply, and then click OK. Excel multiplies each cell by 1, and in doing so, converts the text to numbers.

How do I convert a specific number to text in Excel?

Select all cells with the source strings. On the Extract tool's pane, select the Extract numbers radio button. Depending on whether you want the results to be formulas or values, select the Insert as formula box or leave it unselected (default).

How do I convert a value to text in Excel using keyboard?

Enter an apostrophe (') before any number For example, entering '0123 in a cell shows "0123" (with the zero) in the cell, and is noted as being formatted as text. Enter data this way is a quick way of changing the format of a cell. If you type the same number as above without the apostrophe, it would appear as "123".


1 Answers

This isn't exactly what you want, but for your consideration:

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterSpellOutStyle];

NSString *s = [f stringFromNumber:[NSNumber numberWithInt:932]];
NSLog(@"%@", s);
[f release];

This will log:

nine hundred and thirty-two

Again, it's not the "nine three two" you want, but it's also nice and short. :)

like image 73
Dave DeLong Avatar answered Oct 05 '22 23:10

Dave DeLong