Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Rounding a number to nearest even
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Ivan Illarionov, post: 3534195"] def even_round(x): if x % 1 == .5 and not (int(x) % 2): return float(int(x)) else: return round(x) nums = [ 3.2, 3.6, 3.5, 2.5, -.5, -1.5, -1.3, -1.8, -2.5 ] for num in nums: print num, '->', even_round(num) 3.2 -> 3.0 3.6 -> 4.0 3.5 -> 4.0 2.5 -> 2.0 -0.5 -> 0.0 -1.5 -> -2.0 -1.3 -> -1.0 -1.8 -> -2.0 -2.5 -> -2.0 [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Rounding a number to nearest even
Top