Home > Software engineering >  How to separate the Full number and Decimal Number from a Divison Result in Python
How to separate the Full number and Decimal Number from a Divison Result in Python

Time:07-21

For python

After division, if the result has any decimal number with the full number, I just want to get the full number and ignore the decimal number.

for example:

130/33 = 3.939393

here I just want to use the number "3" and ignore the ".939393"

How can I do that?

Thanks in advance.

CodePudding user response:

Use integer division:

print(130//33)
3

SEE ALSO:

Also referred to as integer division. The resultant value is a whole integer, though the result’s type is not necessarily int. The result is always rounded towards minus infinity: 1//2 is 0, (-1)//2 is -1, 1//(-2) is -1, and (-1)//(-2) is 0

Numeric Types — int, float, complex: https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex


What is the difference between '/' and '//' when used for division?

CodePudding user response:

You can cast to int without using any library

print(int(130/33))

Documentation about casting

CodePudding user response:

You can use floor method of math package.

import math   
  
# prints floor() method
print(math.floor(130/33))

Output:  

3
  • Related