My program prints out HUGE numbers - like 100363443, up to a trillion -- and it sort of hard to read them, so I would like to print any number in easy to read form.
right now I use
printf ("%10ld", number);
format
I would appreciate a resulting number using printf. Most of my code is c++ yet I don't want to introduce std::cout, as I already have printf
thanks
Easy way might be to convert to a double just before output and use %e which will print them in exponential scientific notation. Try this:
Here is an example I wrote in straight C w/o using locale. Only works for positives. (Much help from "DiscoVlad")
Use the non-standard
apostrophe
flag in the printf format string, if you have that option available and don't mind losing a little bit of portability.According to my documentation, the
'
flag is available for POSIX systems since 1997.If you are on Unix, Linux, Mac, ... you should have no problem
If you are on Windows, DOS, iSeries, Android, ... all bets are off (but maybe you can install a POSIX layer to your system).
On my system this program produces:
Remember localization (especiallyif you are writing a library).
In europe (except the UK) it would be 1.000.000 rather than 1,000,000
note that the number is a float
EDIT: or if you don't like scientific I found this on the net:
EDIT 2: As pointed out by Jerry you don't need the comma class as above, this seems sufficient by itself (although there are presumably locales which don't format large numbers at all?):
You could use humanize_number() which uses suffixes like k, m, etc., to leave out low order digits. This is not a standard routine, so you should d/l the source I have linked to. (2-clause BSD license, allows any kind of use.)
Humanize_number man page.
Humanize_number source code from NetBSD.
This works by appending suffixes as follows: