| | |
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 |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp game gameprogramming givemetehcodez graphics gui html ide image int integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list login loops mac map method methods mobile netbeans nonstatic notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor






