I need to print doubles in C, with a width of 20, but using the maximum precision that can be printed in the 20 chars
For example:
-123.123456789123456789
should be printed:
-123.1234567891234568
Up to now I have the following code that works, but is ugly and has a problem:
double num = .......;
int pr = 0;
char temp[32] = { 0 };
char *point = NULL;
sprintf(temp, "%.20f", num);
point = strchr(temp, '.');
if (point) {
pr = 20 - (1 + point - temp);
}
printf("%20.*f", pr, num);
The problem is that this does not work with %g instead of %f, it prints more than 20 characters sometimes, even though no exponential part exists. The problem arises when a number like 0.000123456789123456789 is given. The extra zeroes after the decimal do not seem to count (!).