简体   繁体   English

在Python 2.7中除以浮点数时得到商

[英]Get the quotient when dividing a float in Python 2.7

I want to divide a float by an integer (like 3.87 / 2) and get a quotient (1 in my example). 我想将浮点数除以整数(例如3.87 / 2)并得到商(在我的示例中为1)。 How do I do this in python 2.7? 如何在python 2.7中做到这一点?

The '%' and '/' operators only work with integers '%'和'/'运算符仅适用于整数

简单地说:

int(3.87 / 2)
import math
divisor = math.floor(3.875 / 2.0)
print(divisor)
#1.0

Alternative: 替代方案:

divisor = int(3.875 / 2)
print(divisor)
#1

Or (as mentioned by @Nizil) 或(如@Nizil所述)

divisor = 3.87 // 2
print(divisor)
#1.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM