PDA

View Full Version : Python Rounding



noodleman999
September 18th, 2011, 09:33 PM
So, i need to round a number, but the number can't be a decimal number.
So it can't be fx 40.0, it must be 40
And it's rounding to a precision of 1, so 40.1 would be rounded to 40.
Thanks ahead.

JDShu
September 18th, 2011, 10:10 PM
I would use the round function and then cast the result as an int.

noodleman999
September 19th, 2011, 12:39 PM
I tried, but it didn't work in this case of some reason. Well, it worked, but not fully.
Trying to find out if the problem is something else, which is what I really DON'T hope.

ofnuts
September 19th, 2011, 01:33 PM
Well, it worked, but not fully.Cana you elaborate on that (input and expected/effective results)? And what is your definition of "round"?

noodleman999
September 19th, 2011, 03:21 PM
Okay, i'm currently making a PyGame program, and i want the background to slowly change from black to green, and back.
Therefore, i can't use +1, as it goes too quick
I need to use 0.1
But the pygame.Color function can't take decimals.
After that, i tried:


rgc = round(rgc)
rgc = int(rgc)

And it went to green, fine. But as soon as it tried backwards: Error saying invalid color arguement.

JDShu
September 19th, 2011, 04:43 PM
Without seeing the code, I can't tell what in particular is wrong with, but your approach seems to use floats needlessly. It seems better to use integers throughout, for example only incrementing the color value every 10 passes of the loop.

noodleman999
September 19th, 2011, 05:27 PM
Without seeing the code, I can't tell what in particular is wrong with, but your approach seems to use floats needlessly. It seems better to use integers throughout, for example only incrementing the color value every 10 passes of the loop.
I'll try. Thanks for the tip :D