Home » Python » Python programs

Python | How can I force division to be floating point? Division keeps rounding down to 0?

Here, we are going to learn how can I force division to be floating point? Division keeps rounding down to 0 in Python programming language?
Submitted by Sapna Deraje Radhakrishna, on March 02, 2020

Until the python version 2, the division of two integers was always being rounded down to 0.

Consider the below example, being executed in python version 2.7,

result = 4/5
print(result)

Output

0

Other than overcoming the python version 2.7 which is mostly outdated and not a recommended version, the above-mentioned issue can be resolved while we update the python version to 3.X

result = 4/5
print(result)

Output

0.8

Python Consoles:

Python 2.7.16 (default, Dec 13 2019, 18:00:32) 
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> result = 4/5
>>> print(result)
0
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> result = 4/5
>>> print(result)
0.8






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.








Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.