Java- need help converting value

Status
Not open for further replies.

Skull

Solid State Member
Messages
7
i need to write a porgram prompts the user for a number. The number should be stored by the program as a string, and the program should print the number out in dollar format.

Example:
Enter a dollar amount:
12345678
This is $12,345,678.00


plz help... i was trying to figure this out for the whole day la
 
The float class has a toString() function that will convert a float to a string. From there just parse the string so that it looks like a dollar amount.

If the parsing is giving you troble heres some simple psudo code for it:

String dollars = input.length - 2;
int loop = dollars / 3;
int stringPOS = 0;

System.out.print("$");
for ( int i = 0; i < loop; i++ ) {
for (int j=0; j< 3; j++ ) {
System.out.print(input[stringPOS];
stringPOS++;
}
System.out.print(",");
}
System.out.print("." + input[input.length-2] + input[input.length-1]);

(something like that. I suck writing code without a complier).

Java Bible ->
http://java.sun.com/j2se/1.3/docs/api/
 
There might actually be a currency class somewhere. I know in .NET there is, and I haven't used Java in ages. But I'd look for a localization/internationalization/currency class or package in the Java API. I'm willing to bet that they have something like that. So you wont' have to go through looping through it all yourself.
 
Iron_Cross said:
There might actually be a currency class somewhere. I know in .NET there is, and I haven't used Java in ages. But I'd look for a localization/internationalization/currency class or package in the Java API. I'm willing to bet that they have something like that. So you wont' have to go through looping through it all yourself.

Hmm VB's is something around the lines of: outputHere.Text = yourNumberHere.Parse("C")

... I think, can't remember off the top of my head. I just started in it this year :D.
 
Status
Not open for further replies.
Back
Top Bottom