Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76884 > unrolled thread
| Started by | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| First post | 2014-08-23 13:47 -0400 |
| Last post | 2014-08-23 18:03 -0400 |
| Articles | 20 on this page of 31 — 8 participants |
Back to article view | Back to comp.lang.python
Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 13:47 -0400
Re: Working with decimals Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-23 14:21 -0400
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 15:07 -0400
Re: Working with decimals Joel Goldstick <joel.goldstick@gmail.com> - 2014-08-23 15:22 -0400
Re: Working with decimals Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-23 20:24 +0100
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 15:48 -0400
Re: Working with decimals Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-23 21:31 +0100
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 17:13 -0400
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 22:47 +0100
Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-24 08:31 +1000
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 23:47 +0100
Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-24 08:53 +1000
Re: Working with decimals Larry Hudson <orgnut@yahoo.com> - 2014-08-24 00:04 -0700
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 10:58 -0400
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 11:12 -0400
Re: Working with decimals Larry Hudson <orgnut@yahoo.com> - 2014-08-24 14:24 -0700
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-24 19:07 -0400
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:12 +0100
Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:17 -0600
Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:19 -0600
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:25 +0100
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:29 +0100
Re: Working with decimals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-25 12:16 +1000
Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-25 12:27 +1000
Re: Working with decimals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-25 12:51 +1000
Re: Working with decimals Chris Angelico <rosuav@gmail.com> - 2014-08-25 13:01 +1000
Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:37 -0600
Re: Working with decimals Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-24 13:40 -0600
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-24 20:49 +0100
Re: Working with decimals Joshua Landau <joshua@landau.ws> - 2014-08-23 22:52 +0100
Re: Working with decimals Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-23 18:03 -0400
Page 1 of 2 [1] 2 Next page →
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-23 13:47 -0400 |
| Subject | Working with decimals |
| Message-ID | <vrihv9l5sce3bkreceav5uhkaqdo9dqnri@4ax.com> |
I am trying to do this example:
http://openbookproject.net/pybiblio/practice/wilson/loan.php
The instructions warn that floating point math can get messy so I
cheated a little bit to get me going.
I made my program work by using numbers that wouldn't get messy.
Instead of using 6% interest I used 10 and instead of using 12 months,
I used 10.
I managed to get it working and formatted just like they wanted it,
but now I want to try to use any numbers. It has been hard to figure
out which method to use.
Here is the working program.
import sys
count = 0
payment = 0
borrowed = 100
rate = 10
term = 10
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 (repr(count).rjust(3), repr(payment).rjust(13),
repr(balance).rjust(14))
payment = (borrowed + interest)/term
balance = balance - payment
count = count + 1
What should I use to make the formatting come out correctly when the
program prints "payment" and "balance" using decimal format?
If you change the "rate" from 10 to 6 and the "term" from 10 to 12,
the screen gets very messy.
Anyone care to suggest what method to use to fix the decimal format?
[toc] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2014-08-23 14:21 -0400 |
| Message-ID | <mailman.13346.1408818070.18130.python-list@python.org> |
| In reply to | #76884 |
On Sat, Aug 23, 2014 at 1:47 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> I am trying to do this example:
> http://openbookproject.net/pybiblio/practice/wilson/loan.php
> The instructions warn that floating point math can get messy so I
> cheated a little bit to get me going.
>
> I made my program work by using numbers that wouldn't get messy.
> Instead of using 6% interest I used 10 and instead of using 12 months,
> I used 10.
>
> I managed to get it working and formatted just like they wanted it,
> but now I want to try to use any numbers. It has been hard to figure
> out which method to use.
>
You need to learn about string formatting. Here is a good link:
http://mkaz.com/2012/10/10/python-string-format/
> Here is the working program.
>
> import sys
> count = 0
> payment = 0
> borrowed = 100
> rate = 10
> term = 10
> 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 (repr(count).rjust(3), repr(payment).rjust(13),
> repr(balance).rjust(14))
>
>
I don't understand why you are using repr(..) It is not necessary.
> payment = (borrowed + interest)/term
> balance = balance - payment
> count = count + 1
>
> What should I use to make the formatting come out correctly when the
> program prints "payment" and "balance" using decimal format?
>
> If you change the "rate" from 10 to 6 and the "term" from 10 to 12,
> the screen gets very messy.
>
> Anyone care to suggest what method to use to fix the decimal format?
> --
> https://mail.python.org/mailman/listinfo/python-list
--
Joel Goldstick
http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-23 15:07 -0400 |
| Message-ID | <d6phv9tqakjs6h6p1un36k68uvsu85aku1@4ax.com> |
| In reply to | #76886 |
On Sat, 23 Aug 2014 14:21:03 -0400, Joel Goldstick
<joel.goldstick@gmail.com> wrote:
>On Sat, Aug 23, 2014 at 1:47 PM, Seymore4Head
><Seymore4Head@hotmail.invalid> wrote:
>> I am trying to do this example:
>> http://openbookproject.net/pybiblio/practice/wilson/loan.php
>> The instructions warn that floating point math can get messy so I
>> cheated a little bit to get me going.
>>
>> I made my program work by using numbers that wouldn't get messy.
>> Instead of using 6% interest I used 10 and instead of using 12 months,
>> I used 10.
>>
>> I managed to get it working and formatted just like they wanted it,
>> but now I want to try to use any numbers. It has been hard to figure
>> out which method to use.
>>
>You need to learn about string formatting. Here is a good link:
>http://mkaz.com/2012/10/10/python-string-format/
>
Thanks. I will give that a try.
>> Here is the working program.
>>
>> import sys
>> count = 0
>> payment = 0
>> borrowed = 100
>> rate = 10
>> term = 10
>> 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 (repr(count).rjust(3), repr(payment).rjust(13),
>> repr(balance).rjust(14))
>>
>>
>I don't understand why you are using repr(..) It is not necessary.
>
Would you please change the line to something you feel that is more
appropriate? That was the first command I stumbled on that I could
use to get the column spacing correctly.
I have contacted my local library to get a couple of books on Python,
but at the moment all I have is the web.
Funny, I though using the web would be better than a book. I don't
think so anymore. Using the web, it is hard to find square one
tutorial text.
>> payment = (borrowed + interest)/term
>> balance = balance - payment
>> count = count + 1
>>
>> What should I use to make the formatting come out correctly when the
>> program prints "payment" and "balance" using decimal format?
>>
>> If you change the "rate" from 10 to 6 and the "term" from 10 to 12,
>> the screen gets very messy.
>>
>> Anyone care to suggest what method to use to fix the decimal format?
>> --
>> https://mail.python.org/mailman/listinfo/python-list
I would rather just use Usenet for the moment. Until the group gets
tired of nursing the rookie.
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2014-08-23 15:22 -0400 |
| Message-ID | <mailman.13349.1408821771.18130.python-list@python.org> |
| In reply to | #76889 |
On Sat, Aug 23, 2014 at 3:07 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> On Sat, 23 Aug 2014 14:21:03 -0400, Joel Goldstick
> <joel.goldstick@gmail.com> wrote:
>
>>On Sat, Aug 23, 2014 at 1:47 PM, Seymore4Head
>><Seymore4Head@hotmail.invalid> wrote:
>>> I am trying to do this example:
>>> http://openbookproject.net/pybiblio/practice/wilson/loan.php
>>> The instructions warn that floating point math can get messy so I
>>> cheated a little bit to get me going.
>>>
>>> I made my program work by using numbers that wouldn't get messy.
>>> Instead of using 6% interest I used 10 and instead of using 12 months,
>>> I used 10.
>>>
>>> I managed to get it working and formatted just like they wanted it,
>>> but now I want to try to use any numbers. It has been hard to figure
>>> out which method to use.
>>>
>>You need to learn about string formatting. Here is a good link:
>>http://mkaz.com/2012/10/10/python-string-format/
>>
> Thanks. I will give that a try.
>
>>> Here is the working program.
>>>
>>> import sys
>>> count = 0
>>> payment = 0
>>> borrowed = 100
>>> rate = 10
>>> term = 10
>>> 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 (repr(count).rjust(3), repr(payment).rjust(13),
>>> repr(balance).rjust(14))
>>>
>>>
>>I don't understand why you are using repr(..) It is not necessary.
>>
> Would you please change the line to something you feel that is more
> appropriate? That was the first command I stumbled on that I could
> use to get the column spacing correctly.
> I have contacted my local library to get a couple of books on Python,
> but at the moment all I have is the web.
>
> Funny, I though using the web would be better than a book. I don't
> think so anymore. Using the web, it is hard to find square one
> tutorial text.
>
You should go to python.org and take a look at the tutorial. There
are lots of references to other sites there as well
>
>>> payment = (borrowed + interest)/term
>>> balance = balance - payment
>>> count = count + 1
>>>
>>> What should I use to make the formatting come out correctly when the
>>> program prints "payment" and "balance" using decimal format?
>>>
>>> If you change the "rate" from 10 to 6 and the "term" from 10 to 12,
>>> the screen gets very messy.
>>>
>>> Anyone care to suggest what method to use to fix the decimal format?
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>
> I would rather just use Usenet for the moment. Until the group gets
> tired of nursing the rookie.
> --
> https://mail.python.org/mailman/listinfo/python-list
--
Joel Goldstick
http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-08-23 20:24 +0100 |
| Message-ID | <mailman.13350.1408821901.18130.python-list@python.org> |
| In reply to | #76889 |
On 23/08/2014 20:07, Seymore4Head wrote: > > Funny, I though using the web would be better than a book. I don't > think so anymore. Using the web, it is hard to find square one > tutorial text. > Try typing something like "python string formatting tutorial" into your favourite search engine and you'll find things like https://docs.python.org/3/tutorial/inputoutput.html or http://www.python-course.eu/python3_formatted_output.php For a generalised list try this https://wiki.python.org/moin/BeginnersGuide/Programmers -- 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 | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-23 15:48 -0400 |
| Message-ID | <tlrhv9p501nqu0dki8usrf1fsta3gkqob3@4ax.com> |
| In reply to | #76892 |
On Sat, 23 Aug 2014 20:24:41 +0100, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: >On 23/08/2014 20:07, Seymore4Head wrote: >> >> Funny, I though using the web would be better than a book. I don't >> think so anymore. Using the web, it is hard to find square one >> tutorial text. >> > >Try typing something like "python string formatting tutorial" into your >favourite search engine and you'll find things like >https://docs.python.org/3/tutorial/inputoutput.html or >http://www.python-course.eu/python3_formatted_output.php > Thanks for the links. The python-course looks like a beginner start. It raises one more question. Some have suggested using strings. I understand that strings and numbers are not the same thing. I know that converting numbers to strings can be useful for display, but you would think that there would be enough demand for a built in set of instructions that handle money calculations accurately. >For a generalised list try this >https://wiki.python.org/moin/BeginnersGuide/Programmers
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-08-23 21:31 +0100 |
| Message-ID | <mailman.13353.1408825932.18130.python-list@python.org> |
| In reply to | #76893 |
On 23/08/2014 20:48, Seymore4Head wrote: > > Thanks for the links. The python-course looks like a beginner start. > It raises one more question. > > Some have suggested using strings. I understand that strings and > numbers are not the same thing. I know that converting numbers to > strings can be useful for display, but you would think that there > would be enough demand for a built in set of instructions that handle > money calculations accurately. > The joys of floating point numbers (you're calling them decimals) on computers. Search the archives of this list and you'll find loads of references which will explain them far better than I can. -- 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 | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-23 17:13 -0400 |
| Message-ID | <ek0iv9diq2io1126te9j5pppoilstfuvdo@4ax.com> |
| In reply to | #76884 |
On Sat, 23 Aug 2014 13:47:20 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> wrote:
>I am trying to do this example:
>http://openbookproject.net/pybiblio/practice/wilson/loan.php
>The instructions warn that floating point math can get messy so I
>cheated a little bit to get me going.
>
>I made my program work by using numbers that wouldn't get messy.
>Instead of using 6% interest I used 10 and instead of using 12 months,
>I used 10.
>
>I managed to get it working and formatted just like they wanted it,
>but now I want to try to use any numbers. It has been hard to figure
>out which method to use.
>
>Here is the working program.
>
>import sys
>count = 0
>payment = 0
>borrowed = 100
>rate = 10
>term = 10
>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 (repr(count).rjust(3), repr(payment).rjust(13),
>repr(balance).rjust(14))
>
>
> payment = (borrowed + interest)/term
> balance = balance - payment
> count = count + 1
>
>What should I use to make the formatting come out correctly when the
>program prints "payment" and "balance" using decimal format?
>
>If you change the "rate" from 10 to 6 and the "term" from 10 to 12,
>the screen gets very messy.
>
>Anyone care to suggest what method to use to fix the decimal format?
OK I found the answer to my own question after getting search tips
here.
I found this function that I will be saving for later.
def make_it_money(number):
import math
return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))
(I still need more practice to find out how it does what it does, but
I like the end result)
So I changed the line in question to:
print (repr(count).rjust(3), make_it_money(payment).rjust(13),
make_it_money(balance).rjust(14))
So......now changing the rate from 10 to 6 and the term from 10 to 12
works fine.
I would still like to see other solutions if anyone wants to offer.
Thanks everyone
[toc] | [prev] | [next] | [standalone]
| From | Joshua Landau <joshua@landau.ws> |
|---|---|
| Date | 2014-08-23 22:47 +0100 |
| Message-ID | <mailman.13358.1408830907.18130.python-list@python.org> |
| In reply to | #76902 |
On 23 August 2014 22:13, Seymore4Head <Seymore4Head@hotmail.invalid> wrote:
> def make_it_money(number):
> import math
> return '
> + str(format(math.floor(number * 100) / 100, ',.2f'))
So for one "import math" should never go inside a function; you should
hoist it to the top of the file with all the other imports.
You then have
def make_it_money(number):
return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))
Consider the
'$' + STUFF
This takes your formatted string (something like '12.43') and adds a
"$" to the front.
So then consider
str(format(math.floor(number * 100) / 100, ',.2f'))
The first thing to note is that format is defined like so:
help(format)
#>>> Help on built-in function format in module builtins:
#>>>
#>>> format(...)
#>>> format(value[, format_spec]) -> string
#>>>
#>>> Returns value.__format__(format_spec)
#>>> format_spec defaults to ""
#>>>
format returns a string, so the str call is unneeded.
You then consider that format takes two arguments:
math.floor(number * 100) / 100
and
',.2f'
Looking at the (well hidden ;P) documentation
(https://docs.python.org/3/library/string.html#formatspec) you find:
"The ',' option signals the use of a comma for a thousands separator.
For a locale aware separator, use the 'n' integer presentation type
instead."
and
"The precision is a decimal number indicating how many digits should
be displayed after the decimal point for a floating point value
formatted with'f' and 'F', or before and after the decimal point for a
floating point value formatted with 'g' or 'G'."
So this says "two decimal places with a comma separator."
Then consider
math.floor(number * 100) / 100
This takes a number, say 12345.6789, multiplies it by 100, to say
1234567.89, floors it, to say 1234567 and then divides by 100, to say,
12345.67.
In other words it floors to two decimal places. The one thing to note
is that binary floating point doesn't divide exactly by 100, so this
might not actually give a perfect answer. It'll probably be "good
enough" for your purposes though.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-08-24 08:31 +1000 |
| Message-ID | <mailman.13360.1408833107.18130.python-list@python.org> |
| In reply to | #76902 |
On Sun, Aug 24, 2014 at 7:47 AM, Joshua Landau <joshua@landau.ws> wrote: > So for one "import math" should never go inside a function; you should > hoist it to the top of the file with all the other imports. I'd say "never" is too strong (there are times when it's right to put an import inside a function), but yes, in this case it should really be at the top of the function. However, you won't need the import at all if you let the formatting function do the rounding for you. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Joshua Landau <joshua@landau.ws> |
|---|---|
| Date | 2014-08-23 23:47 +0100 |
| Message-ID | <mailman.13361.1408834114.18130.python-list@python.org> |
| In reply to | #76902 |
On 23 August 2014 23:31, Chris Angelico <rosuav@gmail.com> wrote: > On Sun, Aug 24, 2014 at 7:47 AM, Joshua Landau <joshua@landau.ws> wrote: >> So for one "import math" should never go inside a function; you should >> hoist it to the top of the file with all the other imports. > > I'd say "never" is too strong (there are times when it's right to put > an import inside a function), but yes, in this case it should really > be at the top of the function. But do any of them apply to "import math"? > However, you won't need the import at all if you let the formatting > function do the rounding for you. Can that floor?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-08-24 08:53 +1000 |
| Message-ID | <mailman.13364.1408834894.18130.python-list@python.org> |
| In reply to | #76902 |
On Sun, Aug 24, 2014 at 8:47 AM, Joshua Landau <joshua@landau.ws> wrote: > On 23 August 2014 23:31, Chris Angelico <rosuav@gmail.com> wrote: >> On Sun, Aug 24, 2014 at 7:47 AM, Joshua Landau <joshua@landau.ws> wrote: >>> So for one "import math" should never go inside a function; you should >>> hoist it to the top of the file with all the other imports. >> >> I'd say "never" is too strong (there are times when it's right to put >> an import inside a function), but yes, in this case it should really >> be at the top of the function. > > But do any of them apply to "import math"? Yep. If you have only one function that will ever use it, and that function often won't ever be called, then putting the import inside the function speeds up startup. Anything that cuts down on I/O can give a dramatic performance improvement. Oh, and when I said "top of the function", what I really meant was "top of the file", but I think (hope!) people figured that out. Sorry for the braino. >> However, you won't need the import at all if you let the formatting >> function do the rounding for you. > > Can that floor? I'm not sure, dig into the format spec and see! But was flooring actually a critical part of the problem, or is another sort of rounding just as good? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2014-08-24 00:04 -0700 |
| Message-ID | <SeCdnRbn2NnjE2TOnZ2dnUU7-f2dnZ2d@giganews.com> |
| In reply to | #76902 |
On 08/23/2014 02:13 PM, Seymore4Head wrote:
> On Sat, 23 Aug 2014 13:47:20 -0400, Seymore4Head
>
> I found this function that I will be saving for later.
> def make_it_money(number):
> import math
> return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))
>
> (I still need more practice to find out how it does what it does, but
> I like the end result)
That's total nonsense and overkill! If you really want to do it with a separate function, using
old style:
def make_it_money(number):
return '$%.2f' % number
or using new style:
def make_it_money(number):
return '${:.2f}'.format(number)
But even these functions are unnecessary. Use either of these formatting methods directly in
the print() statement...
>
> So I changed the line in question to:
> print (repr(count).rjust(3), make_it_money(payment).rjust(13),
> make_it_money(balance).rjust(14))
print('{:3d} ${:<13.2f} ${:<14.2f}'.format(count, payment, balance))
or
print('%3d $%-13.2f $%-14.2f' % (count, payment, balance))
But please, please, PLEASE first go through a real tutorial, and WORK the examples to fix them
in your mind. Questions like these will all be covered there. And you'll learn the language as
a whole instead of trying to be spoon-fed isolated answers. It will be well worth your time.
The tutorial on the official Python web site is a good one (of course there are many others)
docs.python.org/3/tutorial/index.html
It does appear that you're using Py3, but in case you're using Py2, change the '3' in that URL
to '2'.
(Print formatting is in section 7)
-=- Larry -=-
PS. Oops, my bad... I just double checked my suggestions, which left-justified the values, but
I see you want them right-justified (which keeps the decimal points lined up). This complicates
it a bit to keep the dollar-sign butted up against the value, and it makes it necessary to use
that make_it_money() function I said was unnecessary. But it's still unnecessary by using a
little different finagling... Try either of these versions:
print('{:3d} {:>13s} {:>14s}'.format(count,
'$' + str(round(payment, 2)), '$' + str(round(balance, 2))))
print('%3d %13s %14s' % (count, '$' + str(round(payment, 2)), '$' + str(round(balance, 2))))
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-24 10:58 -0400 |
| Message-ID | <feujv9158efbofhlua93i1cv3i6ou58bf6@4ax.com> |
| In reply to | #76925 |
On Sun, 24 Aug 2014 00:04:29 -0700, Larry Hudson <orgnut@yahoo.com>
wrote:
>On 08/23/2014 02:13 PM, Seymore4Head wrote:
>> On Sat, 23 Aug 2014 13:47:20 -0400, Seymore4Head
>>
>> I found this function that I will be saving for later.
>> def make_it_money(number):
>> import math
>> return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))
>>
>> (I still need more practice to find out how it does what it does, but
>> I like the end result)
>
>That's total nonsense and overkill! If you really want to do it with a separate function, using
>old style:
>
>def make_it_money(number):
> return '$%.2f' % number
>
>or using new style:
>
>def make_it_money(number):
> return '${:.2f}'.format(number)
>
>But even these functions are unnecessary. Use either of these formatting methods directly in
>the print() statement...
>
>>
>> So I changed the line in question to:
>> print (repr(count).rjust(3), make_it_money(payment).rjust(13),
>> make_it_money(balance).rjust(14))
>
>print('{:3d} ${:<13.2f} ${:<14.2f}'.format(count, payment, balance))
>
>or
>
>print('%3d $%-13.2f $%-14.2f' % (count, payment, balance))
>
>But please, please, PLEASE first go through a real tutorial, and WORK the examples to fix them
>in your mind. Questions like these will all be covered there. And you'll learn the language as
>a whole instead of trying to be spoon-fed isolated answers. It will be well worth your time.
>
>The tutorial on the official Python web site is a good one (of course there are many others)
>
>docs.python.org/3/tutorial/index.html
>
>It does appear that you're using Py3, but in case you're using Py2, change the '3' in that URL
>to '2'.
>
>(Print formatting is in section 7)
>
> -=- Larry -=-
>
>PS. Oops, my bad... I just double checked my suggestions, which left-justified the values, but
>I see you want them right-justified (which keeps the decimal points lined up). This complicates
>it a bit to keep the dollar-sign butted up against the value, and it makes it necessary to use
>that make_it_money() function I said was unnecessary. But it's still unnecessary by using a
>little different finagling... Try either of these versions:
>
>print('{:3d} {:>13s} {:>14s}'.format(count,
> '$' + str(round(payment, 2)), '$' + str(round(balance, 2))))
>
>print('%3d %13s %14s' % (count, '$' + str(round(payment, 2)), '$' + str(round(balance, 2))))
>
Thanks for sharing these. I tried every single one.
The first two you gave didn't format correctly (as you noted) but it
seems like they should have.
If I understand this one:
>print('{:3d} ${:<13.2f} ${:<14.2f}'.format(count, payment, balance))
the ${:<13.2f} part would have lined up correctly if the number had 13
digits before the decimal. The leading 0's were ignored.
The last two hit the spot when the last digit is not a 0. When the
last digit is a 0, it causes the decimals not to line up.
Tiny quirks like that can really be frustrating if you are trying to
deliver a polished product. Luck for me, I am not.
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-24 11:12 -0400 |
| Message-ID | <iovjv9tmb3jp441ltqnamrufn11giet553@4ax.com> |
| In reply to | #76925 |
On Sun, 24 Aug 2014 00:04:29 -0700, Larry Hudson <orgnut@yahoo.com>
wrote:
>On 08/23/2014 02:13 PM, Seymore4Head wrote:
>> On Sat, 23 Aug 2014 13:47:20 -0400, Seymore4Head
>>
>> I found this function that I will be saving for later.
>> def make_it_money(number):
>> import math
>> return '$' + str(format(math.floor(number * 100) / 100, ',.2f'))
>>
>> (I still need more practice to find out how it does what it does, but
>> I like the end result)
>
>That's total nonsense and overkill! If you really want to do it with a separate function, using
>old style:
>
>def make_it_money(number):
> return '$%.2f' % number
>
>or using new style:
>
>def make_it_money(number):
> return '${:.2f}'.format(number)
>
>But even these functions are unnecessary. Use either of these formatting methods directly in
>the print() statement...
>
>>
>> So I changed the line in question to:
>> print (repr(count).rjust(3), make_it_money(payment).rjust(13),
>> make_it_money(balance).rjust(14))
>
>print('{:3d} ${:<13.2f} ${:<14.2f}'.format(count, payment, balance))
>
>or
>
>print('%3d $%-13.2f $%-14.2f' % (count, payment, balance))
>
>But please, please, PLEASE first go through a real tutorial, and WORK the examples to fix them
>in your mind. Questions like these will all be covered there. And you'll learn the language as
>a whole instead of trying to be spoon-fed isolated answers. It will be well worth your time.
>
>The tutorial on the official Python web site is a good one (of course there are many others)
>
>docs.python.org/3/tutorial/index.html
>
>It does appear that you're using Py3, but in case you're using Py2, change the '3' in that URL
>to '2'.
>
>(Print formatting is in section 7)
>
> -=- Larry -=-
>
>PS. Oops, my bad... I just double checked my suggestions, which left-justified the values, but
>I see you want them right-justified (which keeps the decimal points lined up). This complicates
>it a bit to keep the dollar-sign butted up against the value, and it makes it necessary to use
>that make_it_money() function I said was unnecessary. But it's still unnecessary by using a
>little different finagling... Try either of these versions:
>
>print('{:3d} {:>13s} {:>14s}'.format(count,
> '$' + str(round(payment, 2)), '$' + str(round(balance, 2))))
>
>print('%3d %13s %14s' % (count, '$' + str(round(payment, 2)), '$' + str(round(balance, 2))))
>
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.
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2014-08-24 14:24 -0700 |
| Message-ID | <m6WdnfghhcWZxWfOnZ2dnUU7-K-dnZ2d@giganews.com> |
| In reply to | #76932 |
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 -=-
[toc] | [prev] | [next] | [standalone]
| From | Seymore4Head <Seymore4Head@Hotmail.invalid> |
|---|---|
| Date | 2014-08-24 19:07 -0400 |
| Message-ID | <50skv9p5fim24leomdoakv4ug6av54jlre@4ax.com> |
| In reply to | #76946 |
On Sun, 24 Aug 2014 14:24:19 -0700, Larry Hudson <orgnut@yahoo.com>
wrote:
>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 -=-
Yes it is.
Thanks
[toc] | [prev] | [next] | [standalone]
| From | Joshua Landau <joshua@landau.ws> |
|---|---|
| Date | 2014-08-24 20:12 +0100 |
| Message-ID | <mailman.13379.1408907584.18130.python-list@python.org> |
| In reply to | #76902 |
On 23 August 2014 23:53, Chris Angelico <rosuav@gmail.com> wrote: > On Sun, Aug 24, 2014 at 8:47 AM, Joshua Landau <joshua@landau.ws> wrote: >> On 23 August 2014 23:31, Chris Angelico <rosuav@gmail.com> wrote: >>> I'd say "never" is too strong (there are times when it's right to put >>> an import inside a function), but yes, in this case it should really >>> be at the top of the function. >> >> But do any of them apply to "import math"? > > Yep. If you have only one function that will ever use it, and that > function often won't ever be called, then putting the import inside > the function speeds up startup. Anything that cuts down on I/O can > give a dramatic performance improvement. >>> python -c "import time; a = time.time(); import math; b = time.time(); print(b-a)" 0.0005981922149658203 *squints eyes* Is math not already imported by start-up? >>> However, you won't need the import at all if you let the formatting >>> function do the rounding for you. >> >> Can that floor? > > I'm not sure, dig into the format spec and see! FWIW, I haven't seen something that does so.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-08-24 13:17 -0600 |
| Message-ID | <mailman.13380.1408907919.18130.python-list@python.org> |
| In reply to | #76902 |
[Multipart message — attachments visible in raw view] — view raw
On Sun, Aug 24, 2014 at 1:12 PM, Joshua Landau <joshua@landau.ws> wrote: > Is math not already imported by start-up? Why would it be?
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-08-24 13:19 -0600 |
| Message-ID | <mailman.13381.1408908013.18130.python-list@python.org> |
| In reply to | #76902 |
[Multipart message — attachments visible in raw view] — view raw
On Sun, Aug 24, 2014 at 1:17 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote: > > On Sun, Aug 24, 2014 at 1:12 PM, Joshua Landau <joshua@landau.ws> wrote: > > Is math not already imported by start-up? > > Why would it be? It's easy to check, by the way: $ python -c "import sys; print(sys.modules['math'])" Traceback (most recent call last): File "<string>", line 1, in <module> KeyError: 'math'
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web