Re: [Tutor] Want to keep to two decimal places for currency
On Thu, Oct 24, 2013 at 1:21 PM, Marc Tompkins wrote: > On Thu, Oct 24, 2013 at 11:25 AM, bob gailer wrote: > >> On 10/24/2013 2:09 PM, Danny Yoo wrote: >> >>> Related: I saw a picture the other day on Google+ of an mailing envelope >>> whose zip code was written in scientific notation. >>> >> That;s odd - since ZIP codes are character, not integer, >> > > You'd like to think that, wouldn't you? I've dealt with a disturbing > number of programs (some of them from big enough names that you'd think > they'd know better) that chose to represent zip codes as integers. You'd > think that the fact that some of them have to be zero-filled would be a big > clue... but no. > Zip+4 is a HUGE hassle for programs that have chosen to do things this way... Zip+4 has always been optional, but the 5010 format for healthcare billing now requires that all zip codes (except patient's actual home addresses) be represented in 9 glorious digits... that's made a bunch of people scramble, let me tell you! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On Thu, Oct 24, 2013 at 11:25 AM, bob gailer wrote: > On 10/24/2013 2:09 PM, Danny Yoo wrote: > >> Related: I saw a picture the other day on Google+ of an mailing envelope >> whose zip code was written in scientific notation. >> > That;s odd - since ZIP codes are character, not integer, > You'd like to think that, wouldn't you? I've dealt with a disturbing number of programs (some of them from big enough names that you'd think they'd know better) that chose to represent zip codes as integers. You'd think that the fact that some of them have to be zero-filled would be a big clue... but no. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On Thu, Oct 24, 2013 at 2:25 PM, bob gailer wrote: > That;s odd - since ZIP codes are character, not integer, It's not that odd. US Zip codes are a sequence of digits. I've worked with plenty of databases where ZIP codes are held in Numeric columns. It's not the ideal format for ZIP codes, because it's perfectly valid for ZIP codes to start with a 0, and Canadian zip codes have letters in them, but I see it all the time. -- Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On 10/24/2013 2:09 PM, Danny Yoo wrote: Related: I saw a picture the other day on Google+ of an mailing envelope whose zip code was written in scientific notation. That;s odd - since ZIP codes are character, not integer, -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On Thu, Oct 24, 2013 at 9:37 AM, Shelby Martin wrote: > Thank you for your replies. I suspect the solution is a bit more advanced > than where I'm at now, which is chapter 2 of a beginner's book. > Probably because talking about the gory details would completely derail the conversation, as I did. :P My apologies. > Not sure why the author chose to use examples using money calculations > when other calculations that don't need rounding would have sufficed. I > will have to revisit this issue when I'm further along. > I think it's fine to do it when you're learning the language. Just be aware of it, and you should be ok. Related: I saw a picture the other day on Google+ of an mailing envelope whose zip code was written in scientific notation. By computer, of course. Wish I could find it again... Knowing the appropriate representations for data is something you'll pick up as you learn a programming language. Good luck! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
Thank you for your replies. I suspect the solution is a bit more advanced than where I'm at now, which is chapter 2 of a beginner's book. Not sure why the author chose to use examples using money calculations when other calculations that don't need rounding would have sufficed. I will have to revisit this issue when I'm further along. On Wed, Oct 23, 2013 at 8:44 PM, Danny Yoo wrote: > > > > On Wed, Oct 23, 2013 at 4:13 PM, Alan Gauld wrote: > >> On 23/10/13 22:39, Shelby Martin wrote: >> >>> I've looked online but I'm confused - I need to keep it so that the >>> following program limits the output to two decimal places since it deals >>> in currency. >>> >> >> Its generally a bad idea to use floats when dealing with currency since >> they can introduce small errors which can accumulate over time. It's better >> to either use the Decimal module or to convert money to cents/.pennies and >> then convertback to dollars for display purposes. >> > > > Hi Shelby, > > > If you want to read some gory details, you can find the reasons for why > it's unsafe to represent currency with "floating point" numbers. There's a > classic paper called "What Every Computer Scientist Should Know About > Floating-Point Arithmetic": > > http://www.validlab.com/goldberg/paper.pdf > > and you probably don't want to read the whole thing. :P A rough gist of > the problem: traditional, engineering-focused computer math has a > particular trade-off that most programmers do not realize at first: it > trades accuracy for speed. The native hardware of your computer does > calculations in base-2 rather than base-10 arithmetic. Unfortunately, that > means that fractional quantities take a representational hit: your computer > cannot accurately represent certain decimals in base-2. > > It turns out that this floating point arithmetic limitation is not so bad > for a lot of important applications. But it doesn't bode well at all for > applications that deal with money. There are libraries in Python that > avoid using the built-in floating point hardware, such as the Decimal > library that Alan noted. Computations with it are slower, but that's an > acceptable price for getting the dollars and cents right. If we would do > this sort of thing in the real world, we'd use something like the Decimal > library. You'll see equivalent kinds of libraries in other programming > languages, like the BigDecimal class in Java. > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On Wed, Oct 23, 2013 at 4:13 PM, Alan Gauld wrote: > On 23/10/13 22:39, Shelby Martin wrote: > >> I've looked online but I'm confused - I need to keep it so that the >> following program limits the output to two decimal places since it deals >> in currency. >> > > Its generally a bad idea to use floats when dealing with currency since > they can introduce small errors which can accumulate over time. It's better > to either use the Decimal module or to convert money to cents/.pennies and > then convertback to dollars for display purposes. > Hi Shelby, If you want to read some gory details, you can find the reasons for why it's unsafe to represent currency with "floating point" numbers. There's a classic paper called "What Every Computer Scientist Should Know About Floating-Point Arithmetic": http://www.validlab.com/goldberg/paper.pdf and you probably don't want to read the whole thing. :P A rough gist of the problem: traditional, engineering-focused computer math has a particular trade-off that most programmers do not realize at first: it trades accuracy for speed. The native hardware of your computer does calculations in base-2 rather than base-10 arithmetic. Unfortunately, that means that fractional quantities take a representational hit: your computer cannot accurately represent certain decimals in base-2. It turns out that this floating point arithmetic limitation is not so bad for a lot of important applications. But it doesn't bode well at all for applications that deal with money. There are libraries in Python that avoid using the built-in floating point hardware, such as the Decimal library that Alan noted. Computations with it are slower, but that's an acceptable price for getting the dollars and cents right. If we would do this sort of thing in the real world, we'd use something like the Decimal library. You'll see equivalent kinds of libraries in other programming languages, like the BigDecimal class in Java. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to keep to two decimal places for currency
On 23/10/13 22:39, Shelby Martin wrote: I've looked online but I'm confused - I need to keep it so that the following program limits the output to two decimal places since it deals in currency. Its generally a bad idea to use floats when dealing with currency since they can introduce small errors which can accumulate over time. It's better to either use the Decimal module or to convert money to cents/.pennies and then convertback to dollars for display purposes. However, for the purposes of your homework we will ignore that. How do I incorporate that into my current code below? The textbook I'm using doesn't describe how to do that. Thanks in advance. You need tobdistinguish between storage and display. You want to maintain the data stored in its full accuracy but display it with only 2 decimal digits. One waty to do that is with the string format method (I'm assuming Python v3). Check the Python docs for string formatting and you will find several options you can use to control the display of floating point numbers. The basic form is print( "Here is a float: {}".format(123.456789) ) We can limit the field length to 7 digit swith print( "Here is a float: {:15f}".format(123.456789) ) And adjust the number of digits after the point like so: print( "Here is a float: {:7.3f}".format(123.456789) ) You can also specify justification, zero padding and other aspects. Read the docs... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Want to keep to two decimal places for currency
I've looked online but I'm confused - I need to keep it so that the following program limits the output to two decimal places since it deals in currency. How do I incorporate that into my current code below? The textbook I'm using doesn't describe how to do that. Thanks in advance. #Challenge Chapter 2 #Question number three print("This program helps you to determine a tip amount of either") print("15 or 20 percent.") bill=float(input("\nHow much was your restaurant bill total (in dollars)? ")) fifteen=float(bill*0.15) twenty=float(bill*0.2) print("\n15% tip = $", fifteen) print("\n20% tip = $", twenty) input("\n\nPress the enter key to exit.") ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor