# convert number to words..

Page 1 of 1

## 9 Replies - 6127 Views - Last Post: 05 September 2009 - 08:26 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=124097&amp;s=1727cba2b1139af617cb3d2d6b266dc3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 scias23

Reputation: 0
• Posts: 10
• Joined: 17-July 09

# convert number to words..

Posted 05 September 2009 - 10:13 AM

i'm stuck in figuring out the right formula to use in this problem...

i came up with this.
```private static int number, h, o, t;

h = number/100;
t = (number-(h*100))/10;
o = ((number-(h*100))-(t*10));

```

but this can only convert numbers up to 999. what i want to do is to convert numbers up to 9999.

what is the formula for the thousands place? are my formulas correct?

waaah T.T

Is This A Good Question/Topic? 0

## Replies To: convert number to words..

### #2 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: convert number to words..

Posted 05 September 2009 - 10:21 AM

i dont quite understand what are you trying to do?

### #3 scias23

Reputation: 0
• Posts: 10
• Joined: 17-July 09

## Re: convert number to words..

Posted 05 September 2009 - 10:23 AM

mostyfriedman, on 5 Sep, 2009 - 09:21 AM, said:

i dont quite understand what are you trying to do?

i want to convert numbers into words.

### #4 Martyr2

• Programming Theoretician

Reputation: 4666
• Posts: 12,757
• Joined: 18-April 07

## Re: convert number to words..

Posted 05 September 2009 - 10:29 AM

Look for a pattern...

1) 998 / 10? 99 and 998 % 10 = 8
2) 99 / 10? 9 and 99 % 10 = 9
3) 9 is left over

For the number 1288

1) 1288 / 10? 128 and 1288 % 10 = 8
2) 128 / 10? 12 and 128 % 10 = 8
3) 12 / 10? 1 and 12 % 10 = 2
4) 1 is left over

Now think of a loop for this. Perhaps knowing when to end when number < 10. At each step then you can use a switch statement to convert the appropriate number to a word

This post has been edited by Martyr2: 05 September 2009 - 10:30 AM

### #5 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: convert number to words..

Posted 05 September 2009 - 10:42 AM

alright well you can have arrays of strings, one with the tens, and another with the hundreds etc..to convert a number to its corresponding string you will have to play around with division and moduli..for example if you have 999 then you can array[999/100] which will contain "nine hundred" and to get the rest of the number you can array2[999%100] which may contain ninety nine..it all depends on how you arrange your arrays..i of course wont do it like that but i will use the same logic..hope this helps a lil

### #6 scias23

Reputation: 0
• Posts: 10
• Joined: 17-July 09

## Re: convert number to words..

Posted 05 September 2009 - 07:09 PM

what if i want to convert 9999 into words?

mostyfriedman, on 5 Sep, 2009 - 09:42 AM, said:

alright well you can have arrays of strings, one with the tens, and another with the hundreds etc..to convert a number to its corresponding string you will have to play around with division and moduli..for example if you have 999 then you can array[999/100] which will contain "nine hundred" and to get the rest of the number you can array2[999%100] which may contain ninety nine..it all depends on how you arrange your arrays..i of course wont do it like that but i will use the same logic..hope this helps a lil

### #7 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: convert number to words..

Posted 05 September 2009 - 07:14 PM

just use a similar approach

### #8 scias23

Reputation: 0
• Posts: 10
• Joined: 17-July 09

## Re: convert number to words..

Posted 05 September 2009 - 07:29 PM

omg i don't get it.

Martyr2, on 5 Sep, 2009 - 09:29 AM, said:

Look for a pattern...

1) 998 / 10? 99 and 998 % 10 = 8
2) 99 / 10? 9 and 99 % 10 = 9
3) 9 is left over

For the number 1288

1) 1288 / 10? 128 and 1288 % 10 = 8
2) 128 / 10? 12 and 128 % 10 = 8
3) 12 / 10? 1 and 12 % 10 = 2
4) 1 is left over

Now think of a loop for this. Perhaps knowing when to end when number < 10. At each step then you can use a switch statement to convert the appropriate number to a word

### #9 scias23

Reputation: 0
• Posts: 10
• Joined: 17-July 09

## Re: convert number to words..

Posted 05 September 2009 - 08:16 PM

could you please give me a hint of the loop here?

Martyr2, on 5 Sep, 2009 - 09:29 AM, said:

Look for a pattern...

1) 998 / 10? 99 and 998 % 10 = 8
2) 99 / 10? 9 and 99 % 10 = 9
3) 9 is left over

For the number 1288

1) 1288 / 10? 128 and 1288 % 10 = 8
2) 128 / 10? 12 and 128 % 10 = 8
3) 12 / 10? 1 and 12 % 10 = 2
4) 1 is left over

Now think of a loop for this. Perhaps knowing when to end when number < 10. At each step then you can use a switch statement to convert the appropriate number to a word

### #10 carltech

• What did you call me?

Reputation: 28
• Posts: 997
• Joined: 19-October 07

## Re: convert number to words..

Posted 05 September 2009 - 08:26 PM

Think o what number the division or moduli gives you when you compare it to the full number.
EX)
integer division:

9999/10 = 999 nine thousand
999/10 = 99 nine hundred
99/10 = 9 and ninety
9/10 = 0

it took 3 times to simplify the number to the ones place so you know it is 9 thousand

9999%10 = 9 nine