Results 1 to 9 of 9

Thread: How to format this number ?

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    How to format this number ?

    I've like this number 1234567 , I want to format it to be like this : 1,234,567 . I tried atof("% .3",numberxxx) but really what i want ! any help guys ?
    Last edited by Pirate; Jul 7th, 2006 at 09:28 AM.

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370

    Re: How to format this number ?

    Code:
    #include <stdlib.h>
    #include <string.h>
    /* reverse a string */
    void str_rev(char *src)
    {
    	if (*src)
    	{
    		char *p=src;
    		char *q=strchr(src,0);
    		while (--q > p)
    		{
    			char swap=*q;
    			*q=*p;
    			*p=swap;
    			++p;
    		}
    	}
    }
    
    /* insert radix_delimiters into a number */
    char *radix_format(char *dest,const char radix_delimiter,const int value)
    {
    	char working[12]={0x0};
    	char *p=NULL;
    	char *q=NULL;
    	int i=0;
    	int in_val=(int)value;
    	int is_neg=(value < 0);
    
    	if(is_neg)
    		in_val=abs((int)value);
    	sprintf(working,"%d",in_val);
    	str_rev(working);
    	for(i=1,q=dest,p=working;*p;p++,i++)
    	{		
    		*q++=*p;
    		if(!(i%3) && *(p+1) ) *q++=radix_delimiter;
    	}
    	*q=0x0;
    	if(is_neg)
    		strcat(dest,"-");
    	str_rev(dest);
    	return dest;
    }
    
    int main()
    {
    	int i=0;
    	char result[24]={0x0};
    
    	for(i=-500;i<1000000001;)
    	{
    		printf("%d %s\n", i, radix_format(result,',',i));
    		i*= -10;
    	}
    	return 0;
    }

  3. #3

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: How to format this number ?

    Thanks Jim .Though , I was hoping for easier code because i'm using VC++.
    appreciated

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How to format this number ?

    There is easier code. Use a non-C locale and the C++ stream's output streamers.

    Code:
    std::cout.imbue(std::locale("")); // Use system default locale.
    // std::cout.imbue(std::locale("de_DE")); // Use German locale on glibc (Linux) system.
    // std::cout.imbue(std::locale("English_United States")); // Use US-English locale on MSCRT (Win32) system.
    
    std::cout << 12345678 << '\n';
    Output: 12,345,678
    Output with German locale: 12.345.678

    Mind you, the available locales may differ from system to system, and trying to get an unavailable one will throw an exception.

    Now, if you want the result in a string, you can use either a stringstream instead of the std streams, or you can use the locale's num_put facet directly - but the latter is rather complicated, so why do it?
    Code:
    std::ostringstream oss; oss.imbue(std::locale(""));
    oss << 12345678;
    std::string result = oss.str();
    std::cout is in <iostream>, std::locale in <locale>, std::ostringstream in <sstream>.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: How to format this number ?

    Last question , how can I display "result" in a static text ? I tried but it's complaining about "result" for missing ";" in this line (std::string result = oss.str()

    beside other 14 errors .

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How to format this number ?

    Huh? Post the full code?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: How to format this number ?

    Clearly , newbie
    Code:
    void CLoanCalculatorDlg::OnBtnCalculate() 
    {
    
    	UpdateData(TRUE);
    
    	float YearlyGross,TotalAmount,Instalment,RemainingSalary,TotalGross=0;
    	
    
    	if ((m_Loan.GetLength()==0) | (m_Rate.GetLength()==0) | (m_Years.GetLength()==0) | (m_Salary.GetLength()==0))
    		AfxMessageBox("Please Complete any blank fields");
    	
    	else{
    		
    m_LblResultLoan.Format("$%.3f",atof(m_Loan));
    
    YearlyGross=atof(m_Loan) * atof(m_Rate) / 100;
    m_LblYearlyGross.Format("%.3f",YearlyGross);
    
    TotalAmount=YearlyGross * atoi(m_Years) + atof(m_Loan);
    m_LblTotalAmount.Format("%.3f",TotalAmount);
    
    
    Instalment = TotalAmount / (atoi(m_Years) * 12);
    m_LblResultInstallments.Format("%.3f", Instalment);
    
    RemainingSalary = atof(m_Salary) - Instalment;
    m_LblRemainingSalary.Format("%.3f",RemainingSalary);
    
    //Here i where I need to format the number
    TotalGross=YearlyGross * atoi(m_Years);
    m_LblTotalGross.Format("%.3f",TotalGross);	
    
    /*  I tried this but I think it's all wrong.
    std::ostringstream oss; oss.imbue(std::locale("US-English"));
    oss << 1234567;
    std::string result = oss.str();
    m_LblTotalAmount.Format(result);  
    */
    		
    		UpdateData(FALSE);
    	}

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: How to format this number ?

    Format is the wrong function here, but whether you use it or not (you can, it's just a waste of CPU cycles), you need to use c_str():
    Code:
    // I'm assuming the variable is a CString.
    m_LblTotalAmount = oss.str().c_str(); // No need for another variable.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: How to format this number ?

    Thanks , thought I AM still something wrong with the declaration of "oss" . I know I've to hit a lot of C++ books before posting here but oh well...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width