Welcome to Code Sharer, a code gallery for creating, saving and sharing all kinds of code snippets.

Python — A simple rounding function for Python 3

Posted On: Thursday, September 6, 2018 at 11:44 AM Last updated: Thursday, September 6, 2018 at 11:51 AM

There are no library dependencies for this to work.


The syntax can easily be changed to work with Python 2.


To use, include the code after your imports section of your program then type round(x) where you want a number to be rounded. replace x with the float you want to be properly rounded.
def round(x):
y = int(x)
z =y+1
if x-y > z-x:
x = z
print(z)
elif x-y == z-x:
x = z
print(z)
else:
x = y
print(y)

Ryan Bradley-Evans (Admin)

Joined on
Contributed 22 Snips

Get Social and Share

Tags