Possible Duplicate:
Output 1000000 as 1,000,000 and so on
I have a float variable in the format xxxxxxxx.xx (Eg. 11526.99). I'd like to print it as 11,562.99 with a comma. How can I insert a comma in C?
Possible Duplicate:
Output 1000000 as 1,000,000 and so on
I have a float variable in the format xxxxxxxx.xx (Eg. 11526.99). I'd like to print it as 11,562.99 with a comma. How can I insert a comma in C?
Try:
#include <locale.h>
#include <stdio.h>
int main()
{
float f = 12345.67;
// obtain the existing locale name for numbers
char *oldLocale = setlocale(LC_NUMERIC, NULL);
// inherit locale from environment
setlocale(LC_NUMERIC, "");
// print number
printf("%'.2f\n", f);
// set the locale back
setlocale(LC_NUMERIC, oldLocale);
}
This depends on the current locale. The C and POSIX locales do not have a thousands separator. Instead of inheriting the locale from the environment you can set it yourself to a locale that you know uses a thousands separator. On my system, using "en_NZ"
provides a thousands separator.
The below addcommas function is a version locale-less, that allows negative floats (doesn't work with exponent like 3.14E10
though)
#include <stdio.h>
#include <string.h>
#define DOT '.'
#define COMMA ','
#define MAX 50
static char commas[MAX]; // Where the result is
char *addcommas(float f) {
char tmp[MAX]; // temp area
sprintf(tmp, "%f", f); // refine %f if you need
char *dot = strchr(tmp, DOT); // do we have a DOT?
char *src,*dst; // source, dest
if (dot) { // Yes
dst = commas+MAX-strlen(dot)-1; // set dest to allow the fractional part to fit
strcpy(dst, dot); // copy that part
*dot = 0; // 'cut' that frac part in tmp
src = --dot; // point to last non frac char in tmp
dst--; // point to previous 'free' char in dest
}
else { // No
src = tmp+strlen(tmp)-1; // src is last char of our float string
dst = commas+MAX-1; // dst is last char of commas
}
int len = strlen(tmp); // len is the mantissa size
int cnt = 0; // char counter
do {
if ( *src<='9' && *src>='0' ) { // add comma is we added 3 digits already
if (cnt && !(cnt % 3)) *dst-- = COMMA;
cnt++; // mantissa digit count increment
}
*dst-- = *src--;
} while (--len);
return dst+1; // return pointer to result
}
How to call it, for instance (main example)
int main () {
printf ("%s\n", addcommas(0.31415));
printf ("%s\n", addcommas(3.1415));
printf ("%s\n", addcommas(31.415));
printf ("%s\n", addcommas(314.15));
printf ("%s\n", addcommas(3141.5));
printf ("%s\n", addcommas(31415));
printf ("%s\n", addcommas(-0.31415));
printf ("%s\n", addcommas(-3.1415));
printf ("%s\n", addcommas(-31.415));
printf ("%s\n", addcommas(-314.15));
printf ("%s\n", addcommas(-3141.5));
printf ("%s\n", addcommas(-31415));
printf ("%s\n", addcommas(0));
return 0;
}
Compilation instruction example
gcc -Wall comma.c -o comma
Doing
./comma
Should output
0.314150
3.141500
31.415001
314.149994
3,141.500000
31,415.000000
-0.314150
-3.141500
-31.415001
-314.149994
-3,141.500000
-31,415.000000
0.000000
DOT
to what is a dot COMMA
to what is supposed to be a commaMAX
set to 50 assumes the float converted as string will not be more than 49 characters (increase MAX
in doubt)char *a = addcommas(3.1415) ; char *b = addcommas(2.7182) ;
a cannot be used safely anymore after the second call to addcommas