DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   number formatting using recursion (http://www.daniweb.com/forums/thread19899.html)

statc Mar 9th, 2005 10:14 pm
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;
}

BountyX Mar 10th, 2005 12:07 am
Re: number formatting using recursion
 
why complicate things?

import java.text.NumberFormat;
.......
 static String insertCommas(int num)
{
                return NumberFormat.getInstance().format(num);
}

server_crash Mar 10th, 2005 7:16 am
Re: number formatting using recursion
 
I can help you, but I have school right now, but after I'll help you.


All times are GMT -4. The time now is 10:20 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC