how do i round up the the nearest whole number without importing math. Thanks | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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))

12th Nov 2022, 9:28 AM
Steven kervick
Steven kervick - avatar
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
12th Nov 2022, 9:53 AM
Brian
Brian - avatar
+ 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.
12th Nov 2022, 10:24 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
You can use the built-in function round() Examples: round(1.1) output: 1 round(1.5) output: 2
12th Nov 2022, 9:44 AM
Mohamed Ghulam
Mohamed Ghulam - avatar
+ 1
Thanks everyone.I re wrote the code 4 different ways and all worked. I learned alot 👍👍👍
12th Nov 2022, 10:47 AM
Steven kervick
Steven kervick - avatar