``````

In [1]:

def my_round(val, digit=0):
p = 10 ** digit
return (val * p * 2 + 1) // 2 / p

``````
``````

In [2]:

my_round_int = lambda x: int((x * 2 + 1) // 2)

``````
``````

In [3]:

f = 123.456

``````
``````

In [4]:

print(int(my_round(f)))

``````
``````

123

``````
``````

In [5]:

print(my_round_int(f))

``````
``````

123

``````
``````

In [6]:

print(my_round(f, 1))

``````
``````

123.5

``````
``````

In [7]:

print(my_round(f, 2))

``````
``````

123.46

``````
``````

In [8]:

print(int(my_round(0.4)))
print(int(my_round(0.5)))
print(int(my_round(0.6)))

``````
``````

0
1
1

``````
``````

In [9]:

i = 99518

``````
``````

In [10]:

print(int(my_round(i, -1)))

``````
``````

99520

``````
``````

In [11]:

print(int(my_round(i, -2)))

``````
``````

99500

``````
``````

In [12]:

print(int(my_round(i, -3)))

``````
``````

100000

``````
``````

In [13]:

print(int(my_round(4, -1)))
print(int(my_round(5, -1)))
print(int(my_round(6, -1)))

``````
``````

0
10
10

``````
``````

In [14]:

print(int(my_round(-0.4)))
print(int(my_round(-0.5)))
print(int(my_round(-0.6)))

``````
``````

0
0
-1

``````
``````

In [15]:

import math

``````
``````

In [16]:

def my_round2(val, digit=0):
p = 10 ** digit
s = math.copysign(1, val)
return (s * val * p * 2 + 1) // 2 / p * s

``````
``````

In [17]:

print(int(my_round2(-0.4)))
print(int(my_round2(-0.5)))
print(int(my_round2(-0.6)))

``````
``````

0
-1
-1

``````