Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

problem of converting a number into words using double

  Asked By: Rani    Date: Nov 21    Category: Java    Views: 3042

help me with my case study with a problem of converting a
number into words using double. example of it: 1234 to one thousand
two hundred thirty four. tnx in advance! ! !



3 Answers Found

Answer #1    Answered By: Corey Jones     Answered On: Nov 21

write a switch -case statement
String result ="";

case 0: result+="zero";break;
case 1: result+="one"; break;
case 19: result+="nineteen";break;
case 20: result+="twenty"; break;
case 30: result+="thirty";break;
case 40: result+="forty"; break;
case 100: result+="hundred";break;
case 1000: result+="thousan"; break;

and so on

Answer #2    Answered By: Troy Kelley     Answered On: Nov 21

i am giving u a suggestion, just check, whether it is possible? i doubt
if there is any direct solution to it, but we can follow procedure like-

1. take one string array of some comfortable size to store each place's
word format.
2. now start scanning the number  in reverse, & take 2 hash maps- i am
asking u to take 2 hashmaps for the purpose like - whenever u want to translate
a number like 2345 in words- only ten's place will have totally different
wordings like ten, twenty, etc. So, one hashmap to specially store ten's place's
word format & then rest of the places have one , two , three, etc common, just
the thing is u have to append hundred to 3rd place from right(i.e in reverse
order) or thousand to 4th place from right. So, i am taking one hashmap for rest
of the places(excluding ten's place) & then i will use a switch case  to
identify- what i should append to rest of the places.

3.& whenever u will get a word for a single digit, just store it in 1st
place of string array, thus proceed from reverse side of the number & then to
finally obtain it in correct form, u can just print that string array in reverse
from array.lenght to 0.

4. like if i will store - 2345 as in reverse order

1st place of string array- five

2nd place ...............-fourty

3rd place.................-three hundred

4th place ...............-two thousand

& then correct string is obtained by reversing string array, which will
produce correct form- two thousand three hundred fourty five.

ok, i have tried to explain what i think, but not that nice approach,
so, if u get some better way, let me also know, what u will implement.

If you have any comments or questions, submit it on the message board.
NO spam/ads/job posting or you will be BANNED from this group. Exception can be
made it happen by notify me ahead of time.
all new members' message will be verified by me (spam..) before it get posted.

Answer #3    Answered By: Gorkem Yilmaz     Answered On: Nov 21

This is another suggestion.
Convert the number  to string then use the method length() to get the length of
the string
so you know the first number is going to be(thousand if the length equals to
This is a part of the way i think about, and you can easly complete it.

String num = 1234 + ""; // convert the number to string
int len = num.length();
int n; // to store the number to be used in switch
for ( int i = 0; i < num.length(); i++)
n = Integer.parseInt(num.charAt(i));
swicth n
case  1:
if (len = 4)
else if (len = 3)
System.out.println("Hundred"); else if (len = 2)

if (Integer.parseInt(num.CharAt(i+1)) == 0)
System.out.println("and ten");
case 1: System.out.print("eleven"); break;
case 2: ...............................etc.
case 2:
if (len = 4)
else if (len = 3)
System.out.println("Hundreds"); else if (len = 2)

if (Integer.parseInt(num.CharAt(i+1)) == 0)
System.out.println("and twenty");
case 3: ............................... etc.
................................case 9: ..... break;
len--; }
you can handel some stuiations ( as if the number followed by zero like 1045 as
you want e.g: skip the 0 or any way you want)

Didn't find what you were looking for? Find more on problem of converting a number into words using double Or get search suggestion and latest updates.