【问题标题】:Python: is there a way to 'cleanly' divide two numbers of type float and int? [duplicate]Python:有没有办法“干净地”划分两个浮点数和整数? [复制]
【发布时间】:2020-05-23 01:31:10
【问题描述】:

python 新手,面临的问题基本上需要与余数“%”函数相反的问题。例如,如果我想将 81.5 除以 20,我的输出将是 4。我的最佳尝试如下:

amount = 81.504
round(amount, 2)

num20s = amount / 20
int(num20s)

我已经尝试了上述代码的几种不同组合,但到目前为止没有任何效果。这是我最接近我想要的东西,但它在极端情况下不起作用,并且由于某种原因仍然代表最后带有“.0”的数字,所以最后一行不能做任何事情.

【问题讨论】:

  • 听起来你想要的是楼层划分//
  • 是的,这正是我所需要的,谢谢!不过,一切仍以浮点数表示,我将不得不继续努力。
  • 这能回答你的问题吗? Python integer division yields float

标签: python floating-point integer rounding division


【解决方案1】:

python中的整数除法运算符是“//”。

>>> amount = 81.504
>>> amount // 20
Out[3]: 4.0
>>> int(amount // 20)
Out[4]: 4

【讨论】:

  • 整数除法 不就是地板除法吗?
  • 我会使用整数除法和地板除法是同义词。
猜你喜欢
  • 2020-11-26
  • 2016-08-09
  • 2020-06-10
  • 1970-01-01
  • 2017-06-12
  • 2021-02-24
  • 1970-01-01
  • 2022-09-30
  • 2021-12-08
相关资源
最近更新 更多