Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76996
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Subject | Re: Working with decimals part 2 |
| Date | 2014-08-25 19:23 +0100 |
| References | <hsomv9hvf31ntf45rr2ukhb0078cubovre@4ax.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13426.1408991038.18130.python-list@python.org> (permalink) |
On 25/08/2014 18:55, Seymore4Head wrote:
> 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
>
The best approach to trial and error is to use the interactive prompt.
You'll need something like it if you insist on mixing function calls
that contain various types of string formatting with string formatting.
An alternative is to apply the KISS principle.
print ("{x}{y}{z}".format(count, payment, balance)) is all you need,
where x, y and z are the appropriate formatting options for each of
count, payment and balance. These options have already been pointed out
to you.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Working with decimals part 2 Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-25 13:55 -0400
Re: Working with decimals part 2 Chris Angelico <rosuav@gmail.com> - 2014-08-26 04:16 +1000
Re: Working with decimals part 2 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-25 19:23 +0100
Re: Working with decimals part 2 MRAB <python@mrabarnett.plus.com> - 2014-08-25 19:26 +0100
Re: Working with decimals part 2 alex23 <wuwei23@gmail.com> - 2014-08-26 10:46 +1000
Re: Working with decimals part 2 Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-27 19:55 -0400
csiph-web