I wish to output large numbers with thousand-separators (commas or spaces) — basically the same as in How to display numeric in 3 digit grouping but using printf
in C (GNU, 99).
If printf does not support digit grouping natively, how can I achieve this with something like printf("%s", group_digits(number))
?
It must support negative integers and preferably floats, too.
A secure way to format thousand separators, with support for negative numbers:
Because VS < 2015 doesn't implement snprintf, you need to do this
And then
Example usage:
ADD:
Here is a compact way of doing it:
For example, a call to
strmoney(1234567891.4568)
returns the string"1 234 567 891.46"
. You can easily replace the space with another separator (such as a comma) by changing theseparator
variable at the top of the function.If you can use POSIX printf, try