title: printf double with maximum precision in available width
body: <p>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</p>
  
  <p>For example:</p>
  
  <pre><code>-123.123456789123456789 
  </code></pre>
  
  <p>should be printed: </p>
  
  <pre><code>-123.1234567891234568
  </code></pre>
  
  <p>Up to now I have the following code that works, but is ugly and has a problem:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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 (!).</p>