Print leading zeros of a floating point number
<p>I am trying to print something:</p>

<pre><code>&gt;&gt;&gt; print "%02i,%02i,%02g" % (3, 4, 5.66)
03,04,5.66
</code></pre>

<p>However this is not correct. If you notice, the zeroes get correctly pre-pended 
to all the integer floating points (the first two numbers). I need it such that there will be one leading zero if the there is a single digit to the left of the decimal point. </p>

<p>I.e. the solution above should return:</p>

<pre><code>03,04,05.66
</code></pre>

<p>What am I doing wrong?</p>

<p>For <code>g</code>, specify and width and precision:</p>

<pre><code>&gt;&gt;&gt; print "%02i,%02i,%05.3g" % (3, 4, 5.66)
03,04,05.66
</code></pre>

<h3>f versus g</h3>

<p>The difference between <code>f</code> and <code>g</code> is illustrated here:</p>

<pre><code>&gt;&gt;&gt; print "%07.1f, %07.1f, %07.1f" % (1.23, 4567.8, 9012345678.2)
00001.2, 04567.8, 9012345678.2
&gt;&gt;&gt; print "%07.1g, %07.1g, %07.1g" % (1.23, 4567.8, 9012345678.2)
0000001, 005e+03, 009e+09
</code></pre>

<p>When given large numbers, <code>g</code> switches to scientific notation while <code>f</code> just uses more spaces.</p>

<p>Similarly, <code>g</code> switches to scientific notation, when needed, for small numbers:</p>

<pre><code>&gt;&gt;&gt; print "%07.1f, %07.1f, %07.1f" % (.01, .0001, .000001)
00000.0, 00000.0, 00000.0
&gt;&gt;&gt; print "%07.1g, %07.1g, %07.1g" % (.01, .0001, .000001)
0000.01, 00.0001, 001e-06
</code></pre>

python