Reputation: 33405
I'm trying to make printf 'do the right thing' with floating point numbers, i.e.:
Never lose information, always print as many decimals as are needed for the exact value
Don't print a bunch of redundant trailing zeros
Switch to scientific notation if the output would otherwise be unreasonably large
%.20g
seems to do all that, but there's one more thing I would like to get:
Is there any way to get the fourth criterion without losing any of the first three?
Upvotes: 0
Views: 204
Reputation: 1509
It looks like the flag "#" will at least put a period at the end. Try %#.20g
.
The # flag is for example documented here: "Used with a, A, e, E, f, F, g or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written."
Upvotes: 0