+ 1
how do i round up the the nearest whole number without importing math. Thanks
how do i round up the the nearest whole number without importing math. i did y + 0.999 . it did work but doesn't seem the right way. numpaint = int(input()) canv = 40 pt = 5 x = (numpaint * pt) + canv y = (x * 1.1) # 10% y = y + 0.999 # to round up? print(int(y))
4 Answers
+ 3
Mr Steven KervickđșđŠ there is a way to get the smallest floating point number, which is called epsilon. Use that to add *almost* 1.0, but just enough under 1.0 that it won't round up an exact integer.
import sys
rup = 1.0 - sys.float_info.epsilon
...
y = int(y + rup) # to round up
+ 2
Won't truncating and adding 1 work?
x = #some_float
n = 1 + int(x)
There may be the issue that x is already integer. Then rounding should not yield x+1 i suppose. Then check first int(x) == x.
+ 1
You can use the built-in function round()
Examples:
round(1.1)
output: 1
round(1.5)
output: 2
+ 1
Thanks everyone.I re wrote the code 4 different ways and all worked. I learned alot đđđ