+ 31

Can some one tell me how to roud a decimal number into nearest whole number using python. Because 'round()' failed for me. 🐍

By Using PYTHON Eg. 5.6 i need 6 4. 4 i need 4

23rd May 2020, 9:36 AM
Ona Nixon 🇹🇿 👑
Ona Nixon  🇹🇿  👑 - avatar
16 Respostas
+ 6
The tests are a bit weird. There were several reports about solutions that shouldn't work but did. I encountered a few myself. For example here: https://www.sololearn.com/discuss/2116672/?ref=app
23rd May 2020, 10:34 AM
HonFu
HonFu - avatar
+ 20
In C++ both "ceil()" and "round()" Worked, they pass all 5 tests... How Come ??? 😐😐
23rd May 2020, 10:32 AM
Ona Nixon 🇹🇿 👑
Ona Nixon  🇹🇿  👑 - avatar
+ 17
In the solution of "Paint costs" in code coaching.. Did you use round(). I tried multiple times and it fails. Can u help me with that plz ??
23rd May 2020, 9:55 AM
Ona Nixon 🇹🇿 👑
Ona Nixon  🇹🇿  👑 - avatar
+ 15
Ooowh! Thank you for that. I Got this Now. 👍
23rd May 2020, 10:38 AM
Ona Nixon 🇹🇿 👑
Ona Nixon  🇹🇿  👑 - avatar
+ 14
Got it! I tried math.ceil() ... Thank you! 😐 in java and c++ that round() function works. so is round() used to round up in c++ and java. ?
23rd May 2020, 10:14 AM
Ona Nixon 🇹🇿 👑
Ona Nixon  🇹🇿  👑 - avatar
+ 11
That's exactly what the round function does. round(5.6) -> 6 round(4.4) -> 4
23rd May 2020, 9:41 AM
HonFu
HonFu - avatar
+ 6
It is impossible to be fault check your dot.. maybe you used comma
12th Nov 2020, 9:08 PM
Samet
Samet - avatar
+ 5
In paint cost you need to "round up" to the nearest whole number.
23rd May 2020, 10:02 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 5
Either you can use math.ceil() you can use round() to use round the syntax is round (float value to be rounded,no of decimal places upto which it would be rounded)
23rd May 2020, 4:35 PM
Akshay Panwar
Akshay Panwar - avatar
+ 5
But i will prefer you round over ceil
23rd May 2020, 4:38 PM
Akshay Panwar
Akshay Panwar - avatar
+ 4
In C++, you can get ceil from <cmath>. In Java, it's Math.ceil.
23rd May 2020, 10:19 AM
HonFu
HonFu - avatar
+ 4
In order to round up the value, you have to use round() function For instance decimal_no = 343.5567 print(round(decimal_no) print(round(decimal_no, 2) # 2 means the no. gets rounded to two digits Here output: 344 343.56 More info: Like some other suggested ceil() and floor(), let me add some more points. In order to use those functions the math library is used. ceil(x) - It returns the highest value of x floor(x) - It returns the lowest value of x from math import ceil, floor # import math <- This could also be used but use math.func() dec_no = 454.45343 print(ceil(dec_no)) print(floor(dec_no)) Output: 455 454
27th May 2020, 9:55 AM
BENJAMIN LIMBU SUBBA
BENJAMIN LIMBU SUBBA - avatar
+ 4
You can import math And then use math.ceil(x) (Return the ceiling of x, the smallest integer greater than or equal to x.)
10th Jun 2020, 11:20 AM
Mandip Adhikari
Mandip Adhikari - avatar
+ 3
Ona nixon kowero
5th Jul 2020, 2:19 PM
Gēoffrey Eng Åtkinson
Gēoffrey Eng Åtkinson - avatar
+ 2
U can either use ceil() to round off to the up number eg. 3.666 to 4.0 or use floor() to round off to the lower number eg. 3.66 to 3
7th Dec 2020, 10:00 PM
Web Sad
Web Sad - avatar
+ 2
👍
8th Mar 2022, 1:39 PM
Amina Fomina
Amina Fomina - avatar