Hello There, Guest! (LoginRegister)

Post Reply 
Any computer whizzes out there
Author Message
Ranger Offline
Hall of Famer
*

Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
Post: #1
Any computer whizzes out there
I am not a computer whiz, and I am taking a course in computer programming from MIT through Edx. I have to write a Python program but I cannot get the answer in two decimal places. We are dealing with dollars, and I keep getting something like 643.248844844. I tried round(pay, 2), but that does not seem to do any good. I would appreciate any ideas.
(This post was last modified: 02-18-2013 04:29 PM by Ranger.)
02-18-2013 04:28 PM
Find all posts by this user Quote this message in a reply
Advertisement


RiceDoc Offline
Jersey Retired
Jersey Retired

Posts: 7,541
Joined: May 2004
Reputation: 127
I Root For: Rice
Location: Tomball

The Parliament AwardsFootball GeniusNew Orleans BowlCrappiesDonatorsThe Parliament Awards
Post: #2
RE: Any computer whizzes out there
Not a computer whiz, but I think you are on the right track. The command is:

round( x [, n] ) where x is a numeric expression and n is also a numeric expression.

So, for example, round(80.23456, 2) = 80.23 . In your example, if pay=643.248844844, round(pay, 2)=643.25.

See THIS PAGE for more details.
02-18-2013 04:50 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Ranger Offline
Hall of Famer
*

Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
Post: #3
RE: Any computer whizzes out there
(02-18-2013 04:50 PM)RiceDoc Wrote:  Not a computer whiz, but I think you are on the right track. The command is:

round( x [, n] ) where x is a numeric expression and n is also a numeric expression.

So, for example, round(80.23456, 2) = 80.23 . In your example, if pay=643.248844844, round(pay, 2)=643.25.

See THIS PAGE for more details.

Thanks, Doc. I had seen the page. That is the infuriating thing. I use round(pay, 2) and I get something like 643.2288823 Cannot figure out why it is not working correctly. I have experimented with the placement of the round function, and it never seems to make any difference. I have spent more time than I care to think trying to fix this. Figured that I was just doing something wrong somehow, but I guess something weird is going on.
02-18-2013 04:59 PM
Find all posts by this user Quote this message in a reply
texd Offline
Weirdly (but seductively) meaty
*

Posts: 14,447
Joined: Jun 2005
Reputation: 114
I Root For: acorns & such
Location: Dall^H^H^H^H Austin

The Parliament AwardsNew Orleans BowlCrappiesDonatorsThe Parliament Awards
Post: #4
RE: Any computer whizzes out there
Does it work for just round(pay), which should take you to 643? If so, multiply by 100, round, and divide again. A bit redneck but maybe it'll work.
02-18-2013 05:12 PM
Find all posts by this user Quote this message in a reply
Advertisement


Ranger Offline
Hall of Famer
*

Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
Post: #5
RE: Any computer whizzes out there
Texd, thanks, I will try.

Nope. Very strange. I figured the problem might arise from where I placed the function, but I have experimented, and it never changes. I have generally placed it right before the print command. Regardless of whether I print pay as a string or as a number, it still comes out with about ten places to the right of the decimal. This is simply nuts.
(This post was last modified: 02-18-2013 05:24 PM by Ranger.)
02-18-2013 05:21 PM
Find all posts by this user Quote this message in a reply
RiceDoc Offline
Jersey Retired
Jersey Retired

Posts: 7,541
Joined: May 2004
Reputation: 127
I Root For: Rice
Location: Tomball

The Parliament AwardsFootball GeniusNew Orleans BowlCrappiesDonatorsThe Parliament Awards
Post: #6
RE: Any computer whizzes out there
Perhaps you need to define "pay=f(x)" before putting in "pay"? Or go with round(f(pay), 2)?

By the way, I would presume you had already defined "Pay", but I made too many of those type of mistakes during my long ago programming period to presume anything when troubleshooting. I'm thinking plugging the function in where pay is would be my first shot at troubleshooting it.
(This post was last modified: 02-18-2013 05:27 PM by RiceDoc.)
02-18-2013 05:22 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Ranger Offline
Hall of Famer
*

Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
Post: #7
RE: Any computer whizzes out there
Thanks, just tried. The fact that your ideas are not working is convincing me that something is broken somewhere.
02-18-2013 05:28 PM
Find all posts by this user Quote this message in a reply
Advertisement


RiceDoc Offline
Jersey Retired
Jersey Retired

Posts: 7,541
Joined: May 2004
Reputation: 127
I Root For: Rice
Location: Tomball

The Parliament AwardsFootball GeniusNew Orleans BowlCrappiesDonatorsThe Parliament Awards
Post: #8
RE: Any computer whizzes out there
What version of Python are you using? Apparently rounding is done differently in different versions!

if you are in version 3.3, you might try round (pay*100), then divide the result by 100. The round with a single variable will result in an integer, effectively truncating everything beyond the .00 spot.
(This post was last modified: 02-18-2013 05:47 PM by RiceDoc.)
02-18-2013 05:43 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Ranger Offline
Hall of Famer
*

Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
Post: #9
RE: Any computer whizzes out there
Good point about versions. 2.7.3. They do not like 3.00+ because it is apparently not completely backwardly compatible.
02-18-2013 06:03 PM
Find all posts by this user Quote this message in a reply
Ranger Offline
Hall of Famer
*

Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
Post: #10
RE: Any computer whizzes out there
After many hours, got it. Thanks to TexD and Rice Doc for their help.
02-18-2013 06:41 PM
Find all posts by this user Quote this message in a reply
Advertisement


RiceDoc Offline
Jersey Retired
Jersey Retired

Posts: 7,541
Joined: May 2004
Reputation: 127
I Root For: Rice
Location: Tomball

The Parliament AwardsFootball GeniusNew Orleans BowlCrappiesDonatorsThe Parliament Awards
Post: #11
RE: Any computer whizzes out there
So don't keep us in suspense! Tell us what obvious error we were all overlooking!
02-18-2013 07:30 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Ranger Offline
Hall of Famer
*

Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
Post: #12
RE: Any computer whizzes out there
I defined a variable something like

variable = x + y (or whatever)

then I did

round (variable, 2)

The way to get it to work was to define the variable

variable = round(x+y, 2)

I only "figured"it out when one of the students wrote on the discussion forum that his program would not work. He put his program in. I noticed how he had handled it and tried it. Worked perfectly. Still not sure why mine did not.
02-18-2013 08:33 PM
Find all posts by this user Quote this message in a reply
owl7886 Offline
2nd String
*

Posts: 370
Joined: Feb 2007
Reputation: 11
I Root For: the Rice Owls!
Location:

New Orleans BowlDonators
Post: #13
RE: Any computer whizzes out there
I am not familiar with Python specifically, but my guess is that just writing round(variable,2) you were calculating that out to two decimal places, but not doing anything with it like saving it. If you had printed that function, it would probably work as well.
02-18-2013 09:19 PM
Find all posts by this user Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)


Copyright © 2002-2024 Collegiate Sports Nation Bulletin Board System (CSNbbs), All Rights Reserved.
CSNbbs is an independent fan site and is in no way affiliated to the NCAA or any of the schools and conferences it represents.
This site monetizes links. FTC Disclosure.
We allow third-party companies to serve ads and/or collect certain anonymous information when you visit our web site. These companies may use non-personally identifiable information (e.g., click stream information, browser type, time and date, subject of advertisements clicked or scrolled over) during your visits to this and other Web sites in order to provide advertisements about goods and services likely to be of greater interest to you. These companies typically use a cookie or third party web beacon to collect this information. To learn more about this behavioral advertising practice or to opt-out of this type of advertising, you can visit http://www.networkadvertising.org.
Powered By MyBB, © 2002-2024 MyBB Group.