Ranger
Hall of Famer
Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
|
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 |
|
RiceDoc
Jersey Retired
Posts: 7,541
Joined: May 2004
Reputation: 127
I Root For: Rice
Location: Tomball
|
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 |
|
Ranger
Hall of Famer
Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
|
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 |
|
texd
Weirdly (but seductively) meaty
Posts: 14,447
Joined: Jun 2005
Reputation: 114
I Root For: acorns & such
Location: Dall^H^H^H^H Austin
|
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 |
|
Ranger
Hall of Famer
Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
|
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 |
|
Ranger
Hall of Famer
Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
|
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 |
|
Ranger
Hall of Famer
Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
|
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 |
|
Ranger
Hall of Famer
Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
|
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 |
|
Ranger
Hall of Famer
Posts: 10,021
Joined: Jun 2005
Reputation: 48
I Root For: SOF/Owl Basebal
Location:
|
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 |
|
owl7886
2nd String
Posts: 370
Joined: Feb 2007
Reputation: 11
I Root For: the Rice Owls!
Location:
|
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 |
|