Working with decimals part 2

Seymore4Head Seymore4Head at Hotmail.invalid
Mon Aug 25 13:55:32 EDT 2014


import sys
import math
def row1(number):
    return str(number).rjust(3)
def row2(number):
    return str(format(number) ',.2f'))
def row3(number):
    return '${:.2f}'.format(number)
def row4(number):
    return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))

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



More information about the Python-list mailing list