如何将整数转换为浮点数? [英] how to convert an integer to a float?

查看:2392
本文介绍了如何将整数转换为浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能,但''dx = abs(i2 - i1)/ min(i2,

i1)''总是返回0,你能告诉我怎么样我将它从

整数转换成浮点数?

def compareValue(n1,n2):

i1 = int(n1)

i2 = int(n2)

dx = abs(i2 - i1)/ min(i2,i1)

print dx

返回dx< 0.05

解决方案

2月27日下午7:05,ying ... @ gmail.com < ying ... @ gmail.comwrote:


我有以下功能,但''dx = abs(i2 - i1)/ min(i2 ,

i1)''总是返回0,你能告诉我怎样才能将它从

整数转换成浮点数?


def compareValue(n1,n2):

i1 = int(n1)

i2 = int(n2)


dx = abs(i2 - i1)/ min(i2,i1)

print dx

return dx< 0.05



x = x + 0.0


yi ***** @ gmail.com 写道:


我有以下功能,但是''dx = abs(i2 - i1)/ min(i2,

i1)''总是返回0,你能告诉我怎样才能将它转换成

要浮动的整数?



当一个分区中涉及两个整数时,结果也将是一个

分区。如果其中一个操作数是浮点数,那么结果将是一个

浮点数。因此,尝试将其中一个值投射到浮点数之前执行除法:
执行除法:


dx = float(abs(i2 - i1))/ min(i2 ,i1)


-Farshid


Farshid Lashkari写道:


当一个分区涉及两个整数时,结果也将是一个

分区。



我的坏,我的意思是结果也是*整数*


-Farshid


Hi, I have the following functions, but '' dx = abs(i2 - i1)/min(i2,
i1)'' always return 0, can you please tell me how can i convert it from
an integer to float?
def compareValue(n1, n2):
i1 = int(n1)
i2 = int(n2)

dx = abs(i2 - i1)/min(i2, i1)
print dx
return dx < 0.05

解决方案

On Feb 27, 7:05 pm, "ying...@gmail.com" <ying...@gmail.comwrote:

Hi, I have the following functions, but '' dx = abs(i2 - i1)/min(i2,
i1)'' always return 0, can you please tell me how can i convert it from
an integer to float?

def compareValue(n1, n2):
i1 = int(n1)
i2 = int(n2)

dx = abs(i2 - i1)/min(i2, i1)
print dx
return dx < 0.05

x = x + 0.0


yi*****@gmail.com wrote:

Hi, I have the following functions, but '' dx = abs(i2 - i1)/min(i2,
i1)'' always return 0, can you please tell me how can i convert it from
an integer to float?

When two integers are involved in a division, the result will also be a
division. If one of the operands is a float, then the result will be a
float instead. So try casting one of the values to a float before
performing the division:

dx = float(abs(i2 - i1))/min(i2, i1)

-Farshid


Farshid Lashkari wrote:

When two integers are involved in a division, the result will also be a
division.

My bad, I meant the result will also be an *integer*

-Farshid


这篇关于如何将整数转换为浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆