Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP WITH STRINGS

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Hello, I'm a beginier java programmer...my question is how do you covernt a dollar amount into a string. For example, if i've got 156.93 ... how do i say its One hundred fifythree and 93/100. I think i know how to do it kind of but how do i make combinations so it reads it from the command like and includes it all the way up to 99999.99 with all different combinations. Any help would be gladly appreciated.
 
I think that the best way to handle this is to use a for loop and a switch statement to change each single digit in your dollar amount with a String. Some thing like this...

//create a method that takes a String (so you can so String manipulation on your dollar amount

public class ChangeInt() {

public String changeIntToString(String digit){
String single_digit = digit; //local var to hold input
String return_word = null; //retunred word type

switch(single_digit){
case "0"://don't forget the "" since input is String
return_word = "zero";
break;
case "1 ":
return_word = "one ";
break;
case "2":
return_word = "two ";
break;
case "3 " - "9 " likewise....
...
case ".":
return_word = "point " // or whatever
break;
default:
return_word = "whatever your default is...
}

return return_word;
..... etc

Now in your application class you need to find the length of your number and then in a for loop take each number in turn and pass it to this method. Use a StringBuffer to append each word to a finished String.

StringBuffer sb1 = new StringBuffer;
for (int i = 0; i < yourNumberStringHere.length(), i++){

sb1.append(changeIntToString(yourNumberStringHere.charAt);
}

//now you have a StringBuffer with the desired result

String result = new String(sb1); //converts StringBuffer to String

Basically that will give you something like &quot;one two point three three&quot; in a String. You will need to play with formatting options and the like but this is a start.

This sounds like an assignment for class though and I would suggest really studying these concepts hard. This is the most basic of the basics.

Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top