title: Convert float to string in positional format (without scientific notation and false precision)
body: <p>I want to print some floating point numbers so that they're always written in decimal form (e.g. <code>12345000000000000000000.0</code> or <code>0.000000000000012345</code>, not in <a href="https://en.wikipedia.org/wiki/Scientific_notation" rel="noreferrer">scientific notation</a>, yet I'd want to the result to have the up to ~15.7 <a href="https://en.wikipedia.org/wiki/Significant_figures" rel="noreferrer">significant figures</a> of a IEEE 754 double, and no more. </p>
  
  <p>What I want is <strong><em>ideally</em> so that the result is the <em>shortest</em> string in positional decimal format that still results in the same value when converted to a <code>float</code></strong>.</p>
  
  <p>It is well-known that the <code>repr</code> of a <code>float</code> is written in scientific notation if the exponent is greater than 15, or less than -4:</p>
  
  <pre><code>&gt;&gt;&gt; n = 0.000000054321654321
  &gt;&gt;&gt; n
  5.4321654321e-08  # scientific notation
  </code></pre>
  
  <p>If <code>str</code> is used, the resulting string again is in scientific notation:</p>
  
  <pre><code>&gt;&gt;&gt; str(n)
  '5.4321654321e-08'
  </code></pre>
  
  <hr>
  
  <p>It has been suggested that I can use <code>format</code> with <code>f</code> flag and sufficient precision to get rid of the scientific notation:</p>
  
  <pre><code>&gt;&gt;&gt; format(0.00000005, '.20f')
  '0.00000005000000000000'
  </code></pre>
  
  <p>It works for that number, though it has some extra trailing zeroes. But then the same format fails for <code>.1</code>, which gives decimal digits beyond the actual machine precision of float:</p>
  
  <pre><code>&gt;&gt;&gt; format(0.1, '.20f')
  '0.10000000000000000555'
  </code></pre>
  
  <p>And if my number is <code>4.5678e-20</code>, using <code>.20f</code> would still lose relative precision:</p>
  
  <pre><code>&gt;&gt;&gt; format(4.5678e-20, '.20f')
  '0.00000000000000000005'
  </code></pre>
  
  <p>Thus <strong>these approaches do not match my requirements</strong>.</p>
  
  <hr>
  
  <p>This leads to the question: what is the easiest and also well-performing way to print arbitrary floating point number in decimal format, having the same digits as in <a href="https://stackoverflow.com/a/28493269/918959"><code>repr(n)</code> (or <code>str(n)</code> on Python 3)</a>, but always using the decimal format, not the scientific notation.</p>
  
  <p>That is, a function or operation that for example converts the float value <code>0.00000005</code> to string <code>'0.00000005'</code>; <code>0.1</code> to <code>'0.1'</code>; <code>420000000000000000.0</code> to <code>'420000000000000000.0'</code> or <code>420000000000000000</code> and formats the float value <code>-4.5678e-5</code> as <code>'-0.000045678'</code>.</p>
  
  <hr>
  
  <p>After the bounty period: It seems that there are at least 2 viable approaches, as Karin demonstrated that using string manipulation one can achieve significant speed boost compared to my initial algorithm on Python 2.</p>
  
  <p>Thus,</p>
  
  <ul>
  <li>If performance is important and Python 2 compatibility is required; or if the <code>decimal</code> module cannot be used for some reason, then <a href="https://stackoverflow.com/a/38983595/918959">Karin's approach using string manipulation</a> is the way to do it.</li>
  <li>On Python 3, <a href="https://stackoverflow.com/a/38847691/918959">my somewhat shorter code will also be faster</a>.</li>
  </ul>
  
  <p>Since I am primarily developing on Python 3, I will accept my own answer, and shall award Karin the bounty.</p>