Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Float String Formatting Not Working Right
    text
    copied!<p>I'm pretty sure that I'm doing this correctly but not getting the results that I expect from Python 2.7.3 string formatting mini-language. I'm trying to format the output of a float to 3 integer values and a varying number of decimal values. Everything works except the integer values.</p> <p>By itself, the following code works to get me 3 integer values...</p> <pre><code>num = 3 value = '{:03}'.format(num) returns '003' </code></pre> <p>And floats work...</p> <pre><code>num = 3.12345 value = '{:01.2f}'.format(num) returns '3.12' </code></pre> <p>However, combining the two does not seem to work properly. The decimal precision works, but the integer precision does not...</p> <pre><code>num = '3.12345' value = '{:03.2f}'.format(num) returns '3.12' instead of the expected '003.12' </code></pre> <p>The same is true if I try any of the following formats...</p> <pre><code>value = '{:03.02f}'.format(num) - or - value = '{0:3.2f}'.format(num) - or - value = '{:3.02f}'.format(num) - or - value = '{0:3.02f}'.format(num) - or - value = '{0:03.2f}'.format(num) - or - value = '{0:03.02f}'.format(num) - or - value = '{:0&gt;3.2f}'.format(num) etc... Which all return the same '3.12' instead of '003.12' </code></pre> <p>If you're curious about what I'm doing for the varying / dynamic decimal precision, the point is to keep all the values the same length, but different sets of values may not have the same precision. So I take the smaller of the longest precision value or 10 and force the others to match like this..</p> <pre><code>from decimal import Decimal dVals = [ abs(Decimal(val1).as_tuple().exponent), # Get the number of decimal points abs(Decimal(val2).as_tuple().exponent), # "" "" abs(Decimal(val3).as_tuple().exponent), # "" "" abs(Decimal(val4).as_tuple().exponent), # "" "" ] p = max(dVals) # Get the largest value p = min(p, 10) # Get the smaller of the largest value or 10 vals = [val1, val2, val3, val4] for val in vals: value = '{:0&gt;3.{prec}f}'.format(val, prec = p) # do whatever with value here... </code></pre> <p>Again, this all works perfectly, except that the integer value never returns with 3 precision places when I combine it with float precision. So all my efforts to ensure the values output with the same formatted length are all for naught. Ultimately, the output should look similar to...</p> <pre><code>'009.123456700' '180.101010101' '054.987654321' </code></pre> <p>Instead, it looks like this...</p> <pre><code>'9.123456700' '180.101010101' '54.987654321' </code></pre> <p>In other words, ugly. :-|</p> <p>At the very least, I would accept the following output...</p> <pre><code>' 9.123456700' '180.101010101' ' 54.987654321' </code></pre> <p>Any ideas what I'm doing wrong here? Your help is much appreciated!</p> <p>Regards,</p> <p>-RMWChaos</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload