title: Print float/double without trailing zeros?
body: <p>There are a few questions related to this, but I haven't seen one that correctly answers this question. I want to print a floating-point number, but I want the number of decimal places to be adaptive. As an example:</p>
  
  <pre><code>0      -&gt; 0
  1234   -&gt; 1234
  0.1234 -&gt; 0.1234
  0.3    -&gt; 0.3
  </code></pre>
  
  <p>Annoyingly, the <code>%f</code> specifier will only print to a fixed precision, so it will add trailing zeros to all numbers that don't reach that precision. Some have suggested the <code>%g</code> specifier, which works for a set of numbers, but it will switch to scientific notation for some numbers, like this:</p>
  
  <pre><code>printf("%g", 1000000.0); // prints 1e+06
  </code></pre>
  
  <p>How can I print floating-point numbers without the unnecessary zeros, while still maintaining printf's standard accuracy for numbers that actually have fractional components?</p>