I need to round up a floating-point number. For example 4.00011 .
The inbuilt function round()
always rounds up when the number is > .5 and rounds down when <= 5. This is very good.
When I want to round something up (down) I import math
and use the function math.ceil()
(math.floor()
).
The downside is that ceil()
and floor()
have no precision "settings".
So as an R programmer I would basically just write my own function:
def my_round(x, precision = 0, which = "up"):
import math
x = x * 10 ** precision
if which == "up":
x = math.ceil(x)
elif which == "down":
x = math.floor(x)
x = x / (10 ** precision)
return(x)
my_round(4.00018, 4, "up")
this prints 4.0002
my_round(4.00018, 4, "down")
this prints 4.0001
I can't find a question to this (why?). Is there any other module or function I've missed? Would be great to have a huge library with basic (altered) functions.
edit: I do not talk about integers.