Python integer division yields float

前端 未结 5 1497
离开以前
离开以前 2020-11-22 03:01
Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32
Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.
>         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 03:55

    Hope it might help someone instantly.

    Behavior of Division Operator in Python 2.7 and Python 3

    In Python 2.7: By default, division operator will return integer output.

    to get the result in double multiple 1.0 to "dividend or divisor"

    100/35 => 2 #(Expected is 2.857142857142857)
    (100*1.0)/35 => 2.857142857142857
    100/(35*1.0) => 2.857142857142857
    

    In Python 3

    // => used for integer output
    / => used for double output
    
    100/35 => 2.857142857142857
    100//35 => 2
    100.//35 => 2.0    # floating-point result if divsor or dividend real
    

提交回复
热议问题