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?
The below addcommas function is a version locale-less, that allows negative floats (doesn't work with exponent like
3.14E10
though)How to call it, for instance (main example)
Compilation instruction example
Doing
Should output
DOT
to what is a dotCOMMA
to what is supposed to be a commaMAX
set to 50 assumes the float converted as string will not be more than 49 characters (increaseMAX
in doubt)char *a = addcommas(3.1415) ; char *b = addcommas(2.7182) ;
a cannot be used safely anymore after the second call to addcommasTry:
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.