chcw
asked on
Print a number with comma delimited
I want to use printf to print out a number, say 4325932032, with comma delimited in thousands, that is, 4,325,932,032.
There is a way on an online faq telling us to use locale or a code snippet. We just wonder if there is a more reliable and convinient way to do so?
There is a way on an online faq telling us to use locale or a code snippet. We just wonder if there is a more reliable and convinient way to do so?
Hi there,
I would take the number apart by powers of ten, i.e. 10, 100, 1000
but only look at powers dividable by 3, i.e. 1,1000, 1000000, ...
const char *ConvertCD(long Num)
{
bool IsNeg = (Num < 0);
char strNum[32]={""}; //** stores the result
long p(1), Frac; //** Frac stores one three digits fraction of number
char buf[4]; //** temp. buffer for _itoa()
Num = abs(Num); // work on positive values
strcat(strNum, Num < 0 ? "-": "+"); // prefix sign
while (p)
{
p = 3*((int)log10(Num)/3);
Frac = Num/pow(10,p); // contains 3-digit number (* 10^9, 10^6, 10^3, 10^0)
Num -= Frac*pow(10,p);
strcat(strNum, _itoa(Frac, buf, 10)); // this is not ansi .. maybe use sprinf(buf, "%i,", Frac);
if (p>0) strcat(strNum, ",");
}
return strNum;
}
I would take the number apart by powers of ten, i.e. 10, 100, 1000
but only look at powers dividable by 3, i.e. 1,1000, 1000000, ...
const char *ConvertCD(long Num)
{
bool IsNeg = (Num < 0);
char strNum[32]={""}; //** stores the result
long p(1), Frac; //** Frac stores one three digits fraction of number
char buf[4]; //** temp. buffer for _itoa()
Num = abs(Num); // work on positive values
strcat(strNum, Num < 0 ? "-": "+"); // prefix sign
while (p)
{
p = 3*((int)log10(Num)/3);
Frac = Num/pow(10,p); // contains 3-digit number (* 10^9, 10^6, 10^3, 10^0)
Num -= Frac*pow(10,p);
strcat(strNum, _itoa(Frac, buf, 10)); // this is not ansi .. maybe use sprinf(buf, "%i,", Frac);
if (p>0) strcat(strNum, ",");
}
return strNum;
}
There is an API function that will do that - GetNumberFormat
GetNumberFormat
The GetNumberFormat function formats a number string as a number string customized for a specified locale.
int GetNumberFormat(
LCID Locale, // locale
DWORD dwFlags, // options
LPCTSTR lpValue, // input number string
CONST NUMBERFMT *lpFormat, // formatting information
LPTSTR lpNumberStr, // output buffer
int cchNumber // size of output buffer
);
GetNumberFormat
The GetNumberFormat function formats a number string as a number string customized for a specified locale.
int GetNumberFormat(
LCID Locale, // locale
DWORD dwFlags, // options
LPCTSTR lpValue, // input number string
CONST NUMBERFMT *lpFormat, // formatting information
LPTSTR lpNumberStr, // output buffer
int cchNumber // size of output buffer
);
*darn*
;-)
ASKER
hi, AndyAinscow,
Can you provide a sample of usage? I cannot find one in MSDN.
Can you provide a sample of usage? I cannot find one in MSDN.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My suggestion uses a standard windows function to do what was requested.
const char * ConvertCD(long v)
{
static char buf[16];
static char buf2[16];
int i;
int j;
int k;
if (v >= 0)
sprintf (buf, "%d", v);
else
sprintf (buf, "%d", -v);
if ((k = i = strlen (buf)) == 0)
return buf;
i = (i-1) / 3;
for (j = 0; j < i; j++)
{ sprintf (buf2, "%.*s,%s", k - 3*(j+1), buf, buf + k - 3*(j+1));
strcpy (buf, buf2);
}
if (v < 0)
{
sprintf (buf2, "-%s", buf);
return buf2;
}
else
return buf;
}
-MAHESH