Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76993 > unrolled thread
| Started by | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| First post | 2014-08-25 13:55 -0400 |
| Last post | 2014-08-27 19:55 -0400 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-25 13:55 -0400 |
| Subject | Working with decimals part 2 |
| Message-ID | <hsomv9hvf31ntf45rr2ukhb0078cubovre@4ax.com> |
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
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-08-26 04:16 +1000 |
| Message-ID | <mailman.13425.1408990569.18130.python-list@python.org> |
| In reply to | #76993 |
On Tue, Aug 26, 2014 at 3:55 AM, Seymore4Head <Seymore4Head@hotmail.invalid> wrote: > For some reason, it is not working. If I try to use row2 I get this > error: > http://i.imgur.com/FgeF9c9.jpg Several meta-issues. Firstly, your subject line talks about 'decimal' again. You're actually working with floats; Python has a quite separate decimal module, and it's not what you're doing here. It's confusing for those of us who know that you're actually working in binary floating point :) Secondly: An image is a really bad way to capture an error message. Instead of saying "it is not working" and uploading a screenshot to imgur, just copy and paste the exception into the body of the email. Thirdly: If you're working iteratively, keep the files as simple as possible, and if you can do this as one-liners in Idle's interactive mode, that's probably the easiest way to show us what's happening. Don't bury all of them in together into a single program. As to your actual issue... it's a little unclear, because the system's interpretation of what you've written is completely different from yours - due to one misplaced bracket. >>> str(format(number), ',.2f')) You want to format the number .2f, not str the formatted number .2f. I think you can figure it out from there :) ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-08-25 19:23 +0100 |
| Message-ID | <mailman.13426.1408991038.18130.python-list@python.org> |
| In reply to | #76993 |
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
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2014-08-25 19:26 +0100 |
| Message-ID | <mailman.13427.1408991207.18130.python-list@python.org> |
| In reply to | #76993 |
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.
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2014-08-26 10:46 +1000 |
| Message-ID | <ltgle2$ji8$1@dont-email.me> |
| In reply to | #76993 |
On 26/08/2014 3:55 AM, Seymore4Head wrote: > 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. While I don't want to discourage you from learning how to do it the long way, when you have a handle on it I highly recommend using a library for producing tabular data. tabulate is a very handy one, which supports lining up decimal points, amongst many other features: https://pypi.python.org/pypi/tabulate I personally find formatting text to be a pain and will always look for a better method.
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-27 19:55 -0400 |
| Message-ID | <tsrsv9djp8gphf129uc9lqun110oqb2hjj@4ax.com> |
| In reply to | #77010 |
On Tue, 26 Aug 2014 10:46:56 +1000, alex23 <wuwei23@gmail.com> wrote: >On 26/08/2014 3:55 AM, Seymore4Head wrote: >> 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. > >While I don't want to discourage you from learning how to do it the long >way, when you have a handle on it I highly recommend using a library for >producing tabular data. tabulate is a very handy one, which supports >lining up decimal points, amongst many other features: > >https://pypi.python.org/pypi/tabulate > >I personally find formatting text to be a pain and will always look for >a better method. Thanks for this link. For some reason, I didn't see this message the day I started this thread. I am still rechecking questions I have asked. I don't think I am ready for this yet, but hopefully soon.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web