Working with decimals part 2

MRAB python at mrabarnett.plus.com
Mon Aug 25 14:26:39 EDT 2014


On 2014-08-25 18:55, Seymore4Head wrote:
> import sys
> import math
> def row1(number):
>      return str(number).rjust(3)
> def row2(number):
>      return str(format(number) ',.2f'))

That line has to many ')'.

The result of 'format' is a string, so there's no need to use 'str'.

> def row3(number):
>      return '${:.2f}'.format(number)
> def row4(number):
>      return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))

Here, again, the result of 'format' is a string, so there's no need to 
use 'str'.
>
> count = 0
> payment = 0
> borrowed = 100
> rate = 6
> term = 12
> interest=borrowed*rate*.01   #(*1)
> balance = borrowed + interest
> print ("Loan calculator")
> print ("")
> print ("Amount borrowed: ", borrowed)
> print ("Interest rate: ", rate)
> print ("Term: (months)", term)
> print ("")
> print ("Amount borrowed:" , borrowed)
> print ("Total interest paid:" , interest)
> print ("")
> print ("")
> print ("            Amount      Remaining")
> print ("Pymt#        Paid        Balance")
> print ("-----       ------       ----------")
> while count <=term:
>
>      print ("{}         {}         {}".format(row1(count),
> row2(payment),row3(balance)))
>
>      payment = (borrowed + interest)/term
>      balance = balance - payment
>      count = count + 1
>
>
> I changed the program just a little to give myself a little practice
> with number formats.  The main thing I wanted to do was make the
> decimal points line up.  The problem I am having is with the print
> (count)(payment)(balance) line.
>
> I added 4 functions row1-4 for some practice in formatting.
> Row4 is the old makeitmoney function.  I am not using it, but I am
> keeping it in.
>
> row2 is row4 with:
> (math.floor(number * 100) / 100, ',.2f')
> taken out leaving    ',.2f'
> For some reason, it is not working.  If I try to use row2 I get this
> error:
> http://i.imgur.com/FgeF9c9.jpg
>
> Most of my learning is trial and error.  Mostly error.  To try to get
> the decimals to line up, I changed row3 from'${:.2f}' to '${:6.2f}'.
> That makes the decimals line up, but it adds another problem.
> http://i.imgur.com/1KsP3ga.jpg
>
> If you change "borrowed" from 100 to 1000 the fix gets broken again.
> So I changed the '${:6.2f}' to '${:8.2f}'
> http://i.imgur.com/74C5sAx.jpg
>
> That works until you change "borrowed" to 1000000
> http://i.imgur.com/fCuwOXv.jpg
> Is there a way to fix the decimal point to line up without having to
> limit the whole digits?
>
> BTW I changed row3 back to  '${:6.2f}' and used 1 000 000 000 for
> "borrowed"  It doesn't lose any digits in the whole number column, but
> it does skew the formatting.
> http://i.imgur.com/Hjpkts4.jpg
>
There are 2 steps:

1. Format the amount as a string. This is best done in a function.

2. Right-justify the string. This could be done as part of the format
for the row.




More information about the Python-list mailing list