title: How to format a floating number to maximum fixed width in Python
body: <p>I am writing an input file to a program with roots in the 60s, and it reads data from fixed-width data fields on text files. The format is:</p>
  
  <ul>
  <li>field width 8 characters</li>
  <li>floating point numbers must contain a <code>'.'</code> or be written on exponential format, e.g. <code>'1.23e8'</code></li>
  </ul>
  
  <p>The closest i've gotten is</p>
  
  <pre><code>print "{0:8.3g}".format(number)
  </code></pre>
  
  <p>which yields <code>'1.23e+06'</code> with <code>1234567</code>, and <code>'    1234'</code> with <code>1234</code>.</p>
  
  <p>I would like to tweak this however, to get </p>
  
  <ul>
  <li><code>'1234567.'</code> with <code>1234567</code> (i.e. not going to exponential format before it is
  required), </li>
  <li><code>'   1234.'</code> with <code>1234</code> (i.e. ending with a dot so it is not interpreted as an integer), </li>
  <li><code>'1.235e+7'</code> with <code>12345678</code> (i.e. using only one digit for the exponent), </li>
  <li><code>'-1.23e+7'</code> with <code>-1234567</code> (i.e. not violating the 8 digit maximum on
  negative numbers).</li>
  </ul>
  
  <p>Since this is (as far as I recall) easily achievable with Fortran and the problem probably comes up now and then when interacting with legacy code I suspect that there must be some easy way to do this?</p>