Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Java- need help converting value
Closed Thread
Old 01-02-2007, 04:08 PM   #1 (permalink)
 
Newb Techie

Join Date: Sep 2006

Posts: 7

Skull

Send a message via AIM to Skull
Default Java- need help converting value

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
Skull is offline  
Old 01-02-2007, 04:23 PM   #2 (permalink)
Yek
 
T.F's Resident Cool Guy...

Join Date: Aug 2006

Posts: 1,625

Yek is on a distinguished road

Send a message via AIM to Yek Send a message via MSN to Yek Send a message via Yahoo to Yek Send a message via Skype™ to Yek
Default

So changing a number to a string yes?
Yek is offline  
Old 01-02-2007, 04:41 PM   #3 (permalink)
 
Super Techie

Join Date: Jul 2004

Posts: 375

Nikkon

Default

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/
__________________
AMD 3800+ X2
Zalman CNPS7000B CPU Heatsink/Fan
ASUS A8N Premium
2 GB G. Skill DDR 400 (2 X 1 GB)
1 GB Ultra DDR 400 (2 X 512 GB)
eVGA 7800GT 256MB
120 GB seagate HD (7200RPM)
80 GB Seagate HD (7200RPM)
Sound Blaster Audigy 2 Platium ZS Sound Card
Logitec z5300 5.1 THX certified speaker
Medusa 5.1 Headphones
ATI TV turner card
DVD Drive
CD Burner Drive
Razer Copperhead Gaming mouse
Nikkon is offline  
Old 01-03-2007, 11:45 AM   #4 (permalink)
 
Ultra Techie

Join Date: Sep 2003

Location: Bamberg, Germany

Posts: 549

Iron_Cross

Send a message via ICQ to Iron_Cross Send a message via MSN to Iron_Cross Send a message via Yahoo to Iron_Cross
Default

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.
__________________

See today\'s Penny-Arcade!(May contain foul lanuage)
Pain is weakness leaving the body.

PM Me for my MSN
Iron_Cross is offline  
Old 01-04-2007, 10:15 PM   #5 (permalink)
 
Newb Techie

Join Date: Sep 2006

Posts: 7

Skull

Send a message via AIM to Skull
Default

thx, it worked mostly, only need to fix something...
Skull is offline  
Old 01-20-2007, 11:24 AM   #6 (permalink)
 
True Techie

Join Date: Apr 2005

Location: Ontario, Canada

Posts: 182

blackhawk13 is on a distinguished road

Send a message via AIM to blackhawk13
Default

Quote:
Originally posted by Iron_Cross
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 .
blackhawk13 is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On