Formatting negative fixed-length string
<p>In <code>python</code>, I'm trying to format a number to be a fixed-length string with leading zeros, which can be done like so:</p>

<pre><code>'{:0&gt;10}'.format('10.0040')
'00010.0040'
</code></pre>

<p>I have a negative number and want to express the negative, I would get this:</p>

<pre><code>'{:0&gt;10}'.format('-10.0040')
'00-10.0040'
</code></pre>

<p>If I wanted to format the string to be:</p>

<pre><code>'-0010.0040'
</code></pre>

<p>how could I do this?  </p>

<p>I could do an if/then, but wondering if format would handle this already. </p>

<p>I don't know how to do it with <code>str.format</code>.  May I propose using <a href="https://docs.python.org/2/library/string.html#string.zfill" rel="nofollow"><code>str.zfill</code></a> instead?</p>

<pre><code>&gt;&gt;&gt; '-10.0040'.zfill(10)
'-0010.0040'
&gt;&gt;&gt; '10.0040'.zfill(10)
'00010.0040'
</code></pre>

<p>If you can bear converting to a number before formatting:</p>

<pre><code>&gt;&gt;&gt; from decimal import Decimal
&gt;&gt;&gt; '{:010.4f}'.format(Decimal('10.0040'))
'00010.0040'
&gt;&gt;&gt; '{:010.4f}'.format(Decimal('-10.0040'))
'-0010.0040'
</code></pre>

python