Python: Remove division decimal

前端 未结 7 2084
予麋鹿
予麋鹿 2020-12-29 21:05

I have made a program that divides numbers and then returns the number, But the thing is that when it returns the number it has a decimal like this:

2.0
         


        
7条回答
  •  不思量自难忘°
    2020-12-29 21:31

    When a number as a decimal it is usually a float in Python.

    If you want to remove the decimal and keep it an integer (int). You can call the int() method on it like so...

    >>> int(2.0)
    2
    

    However, int rounds down so...

    >>> int(2.9)
    2
    

    If you want to round to the nearest integer you can use round:

    >>> round(2.9)
    3.0
    >>> round(2.4)
    2.0
    

    And then call int() on that:

    >>> int(round(2.9))
    3
    >>> int(round(2.4))
    2
    

提交回复
热议问题