Python | 如何强制除法为浮点数?除数一直舍入为0?
在python版本2之前,两个整数的除法总是四舍五入为0。
考虑下面的示例,该示例在python版本2.7中执行,
result = 4/5print(result)
输出结果
0
除了克服python版本2.7(大多数版本已过时,而不是推荐的版本)以外,在将python版本更新为3.X时可以解决上述问题
result = 4/5print(result)
输出结果
0.8
Python控制台:
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
热门推荐
10 结婚转账祝福语高级简短
11 简短祝福语结婚蛋糕鲜花
12 口才课期末祝福语简短
13 信件祝福语简短情话短句
14 见证求婚文案祝福语简短
15 摩托姐姐的祝福语简短
16 送去考试祝福语简短英文
17 田婆婆生日祝福语简短
18 信件告别祝福语大全简短