| | |
number formatting using recursion
![]() |
•
•
Join Date: Mar 2005
Posts: 2
Reputation:
Solved Threads: 0
hi, i was wondering if anyone could help me out? i have this project in my java class where i need to format an integer to a string with commas (ex:1000000->1,000,000) using recursion. I've tried it for 4 class periods and i just can't figure it out. If you feel guilty by giving me just the answer feel free to give me a tutorial on recursion so that i get the answer and understand it better.
I figured some people woudl like to see what i have now.
I figured some people woudl like to see what i have now.
Java Syntax (Toggle Plain Text)
static String insertCommas(int num) { int rem=0; String str=""; if(num<1000) { str+=num; return str; } else { str+=num+","; insertCommas(num%1000); } return str; }
Last edited by statc; Mar 9th, 2005 at 10:22 pm. Reason: added code
why complicate things?
.......
Java Syntax (Toggle Plain Text)
import java.text.NumberFormat;
Java Syntax (Toggle Plain Text)
static String insertCommas(int num) { return NumberFormat.getInstance().format(num); }
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
![]() |
Similar Threads
- Java and recursion (Java)
- Number formatting/Help with doubles (Java)
- Estimate value of e (C)
- Complete List of Mysql Commands for PHP tokenization script. (PHP)
Other Threads in the Java Forum
- Previous Thread: Executable program design question....
- Next Thread: Question about URLConnection
| Thread Tools | Search this Thread |
2dgraphics @param account affinetransform android api apple applet application arc arguments array automation banking binary binarytree bluetooth chatprogramusingobjects class client code color compare component count database derby design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image inheritance j2me java java.xls javadesktopapplications javaprojects jlabel jni jpanel julia keytool keyword linux list macintosh map method methods midlethttpconnection mobile monitoring netbeans nullpointerexception object open-source pong problem producer program project projectideas property reference replaysolutions ria rim scanner server set size sms sourcelabs splash sql stop swing terminal threads transforms tree ui unicode validation web windows






