Working with decimals

Larry Hudson orgnut at yahoo.com
Sun Aug 24 17:24:19 EDT 2014


On 08/24/2014 08:12 AM, Seymore4Head wrote:
[snip]
> I almost moved, but I was looking at the print out again for this one:
> print('%3d $%-13.2f $%-14.2f' % (count, payment, balance))
>
> I can't understand why the $%-13.2f is pushed against the first
> column, but the $%-14.2f is not.  It seems like the first case ignores
> the leading 0s and the second case doesn't not.
>

Let's break down the %-13.2f format code for example...

-   Left justify (which was my original oversight)
13  Total field width, ie. 13 characters wide (default filler is space)
.2  Round to two decimal places -- that's 3 characters of the 13
         (the decimal point plus the 2-digit fraction)
f   The input is a floating point number

So the last column follows the 13-character wide second column...
(For illustration, I'm using '-' instead of the default space filler)

[-]  [-----------]  [------------]       <-- showing field widths
-12 $123.45------- $15.00---------

Notice, the first field is right-justified by default.
(And that is not negative 12, the '-' is my pretend filler)

Helpful?

      -=- Larry -=-




More information about the Python-list mailing list