+ 1

Why does floats in python round always result to a rounded down figure even if the digit before the decimal point is (>=5)

Python programming challenges

3rd Nov 2020, 2:59 PM
Wizking
Wizking - avatar
9 odpowiedzi
+ 2
Wizking754👻 this is usually working of each programming language, if you convert it to an integer in this way, the digits after decimal point will be cutted off: number=5.7 print(int(number)) # >> 5
3rd Nov 2020, 4:32 PM
JaScript
JaScript - avatar
+ 11
Wizking754, Your statement is not correct: print(round(3.14)) gives 3 print(round(3.77)) gives 4 May be you should share your code that creates your result here. Thanks!
3rd Nov 2020, 3:06 PM
Lothar
Lothar - avatar
+ 5
It doesn't. The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals. Maybe you mean int(). That is casting, not rounding. Use round(f) if you want the nearest integer to f
3rd Nov 2020, 3:10 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Wizking754👻 use round() for rounding, not int(). Read my answer again
3rd Nov 2020, 4:48 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 1
Wizking754👻 You are casting to Integer equalent. This is Rounding : number=5.7 print(round(number))
3rd Nov 2020, 5:06 PM
Jayakrishna 🇮🇳
0
Proof?
3rd Nov 2020, 3:06 PM
Jayakrishna 🇮🇳
0
Benjamin Jürgens probably didnt fully explain myself well. Below is my question https://code.sololearn.com/c3t2A7rHzMo9/?ref=app
3rd Nov 2020, 4:20 PM
Wizking
Wizking - avatar
0
Benjamin Jürgens rather than getting 6 it still results to 5. Hope my question is understood Lothar Jayakrishna🇮🇳 https://code.sololearn.com/c3t2A7rHzMo9/?ref=app
3rd Nov 2020, 4:24 PM
Wizking
Wizking - avatar