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 ?
Printable View
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 ?
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;
}
Thanks Jim .Though , I was hoping for easier code because i'm using VC++.
appreciated
There is easier code. Use a non-C locale and the C++ stream's output streamers.
Output: 12,345,678Code: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 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?
std::cout is in <iostream>, std::locale in <locale>, std::ostringstream in <sstream>.Code:std::ostringstream oss; oss.imbue(std::locale(""));
oss << 12345678;
std::string result = oss.str();
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 :(.
Huh? Post the full code?
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);
}
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.
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...