Python | 如何强制除法运算为浮点数? 除数一直舍入为0?

柔情只为你懂 2023-03-05 05:30 109阅读 0赞

Until the python version 2, the division of two integers was always being rounded down to 0.

在python版本2之前, 两个整数除法总是四舍五入为0

Consider the below example, being executed in python version 2.7,

考虑下面的示例,该示例在python版本2.7中执行,

  1. result = 4/5
  2. print(result)

Output

输出量

  1. 0

Other than overcoming the python version 2.7 which is mostly outdated and not a recommended version, the above-mentioned issue can be resolved while we update the python version to 3.X

除了克服python版本2.7(大多数版本已过时,而不是推荐的版本)以外,在将python版本更新为3.X时,可以解决上述问题

  1. result = 4/5
  2. print(result)

Output

输出量

  1. 0.8

Python Consoles:

Python控制台:

  1. Python 2.7.16 (default, Dec 13 2019, 18:00:32)
  2. [GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.32.4) (-macos10.15-objc-s on darwin
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> result = 4/5
  5. >>> print(result)
  6. 0
  7. Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
  8. [Clang 6.0 (clang-600.0.57)] on darwin
  9. Type "help", "copyright", "credits" or "license" for more information.
  10. >>> result = 4/5
  11. >>> print(result)
  12. 0.8

翻译自: https://www.includehelp.com/python/how-can-i-force-division-to-be-floating-point-division-keeps-rounding-down-to-0.aspx

发表评论

表情:
评论列表 (有 0 条评论,109人围观)

还没有评论,来说两句吧...

相关阅读

    相关 点数运算

    今天学习了浮点数运算(加减乘除)。浮点数运算主要包括两部分:指数运算和尾数运算。在IEEE754标准下,指数运算就是阶码的运算,类似于无符号数运算。尾数运算是原码运算。之前一直