954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

number formatting using recursion

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.

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;
}
statc
Newbie Poster
2 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

why complicate things?

import java.text.NumberFormat;



.......

static String insertCommas(int num)
{
		 return NumberFormat.getInstance().format(num);
}
BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 

I can help you, but I have school right now, but after I'll help you.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You