|
Search Tech-Forums - link takes you to our Forum's search page. Note: The following is only a text archive! To view the actual forum discussion, please visit our website at http://www.tech-forums.net Pages:1 Java- need help converting value(Click here to view the original thread with full colors/images)Posted by: Skull 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 Posted by: Tkey So changing a number to a string yes? Posted by: Nikkon 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 -> [url]http://java.sun.com/j2se/1.3/docs/api/[/url] 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. Posted by: Skull thx, it worked mostly, only need to fix something... Posted by: blackhawk13 [QUOTE][i]Originally posted by Iron_Cross [/i] [B]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. [/B][/QUOTE] 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. vBulletin Copyright ©2000 - 2003, Jelsoft Enterprises Limited. PPC Management vB Easy Archive Final - Created by Xenon |