zoukankan      html  css  js  c++  java
  • python2精确除法

    python2和python3除法的最大区别:

    python2:

    print 500/1000

    python2结果:取整数部分,小数并没有保留

    0
    
    Process finished with exit code 0

    python3:

    print 500/1000

    python3结果:得到真实结果,小数保留

    0.5
    
    Process finished with exit code 0

    那么,如果python2想保留小数部分,要怎么做呢?

    只需要增加一个导入包.就可以了.并不需要其它操作

    from __future__ import division #用于/相除的时候,保留真实结果.小数

    增加导入包后的,python2操作:

    #coding:utf-8
    from __future__ import division
    
    print 500/1000

    结果:

    0.5
    
    Process finished with exit code 0

    还有另一种方式.将除数或被除数两个其它至少一个转换成float型:

    print float(500)/1000

    结果:

    0.5
    
    Process finished with exit code 0
  • 相关阅读:
    unity3D相机缓慢看向目标物体
    设计原则
    unity3D中GUI标题内文字滚动效果
    python3.6中安装PyQt报错
    codeforces 515B.Drazil and His Happy Friends
    HDU 1029 Ignatius and the Princess IV
    POJ 1052 Plato's Blocks
    Uva220 Othello
    uva201 Squares
    uva1587 Box
  • 原文地址:https://www.cnblogs.com/yhleng/p/9223944.html
Copyright © 2011-2022 走看看