Re: Any easy to printf an interger in 9,999, 99 format?



"I_have_nothing" <ddt_pest@xxxxxxxxxxxx> writes:

>Is there any easy way to printf an integer in a way like
>1,234,567?
>I know "%d" can be usd to print it as 1234567.
>Any type field in format specification can do that?
>Or any easy way to do that?

>How about to do that same thing in float type?
>such as make 987564321.85 into 987,564,321.85 ?


An off-topic, non-locale-ized function for integers.
You'll need to modify it for floating point:

--
Chris.


#define N_COMMABUFS 4

char *format64(int64_t value)
{
static char result[N_COMMABUFS][32];
static int whichbuf = 0;

char *rp = &result[whichbuf][0];

whichbuf = (whichbuf+1) % N_COMMABUFS;

sprintf(rp, "%lld", value);
if(output_commas) {
char buf[32], *b=buf, *r=rp;
int i, len;

if(rp[0] == '-') {
*b++ = '-';
++r;
}

len = strlen(r);
for(i=0 ; i<len ; ++i) {
*b++ = *r++;
if(*r && ((len-i)%3) == 1)
*b++ = ',';
}
*b = '\0';
strcpy(rp, buf);
}
return(rp);
}
.



Relevant Pages