除以整数...首先转换为浮点数? [英] Dividing integers...Convert to float first?
问题描述
我仍然是Python的新手。我正在写一个接受
三位整数作为参数的函数。我需要将第一个整数除以
第二个整数,然后得到一个浮点数。我不希望
的调用者必须传递浮点数而不是整数。我如何将
转换传递给函数的参数转换为浮点数才能进行分割?这是必要的,还是他们更好的方式?
谢谢,
Scott Huey
I''m still pretty new to Python. I''m writing a function that accepts
thre integers as arguments. I need to divide the first integer by te
second integer, and get a float as a result. I don''t want the caller of
the function to have to pass floats instead of integers. How do I
convert the arguments passed to the function into floats before I do
the division? Is this necessary, or is their a better way?
Thanks,
Scott Huey
推荐答案
re ***** ***********@gmail.com schrieb:
re****************@gmail.com schrieb:
我仍然是Python的新手。我正在写一个接受
三位整数作为参数的函数。我需要将第一个整数除以
第二个整数,然后得到一个浮点数。我不希望
的调用者必须传递浮点数而不是整数。我如何将
转换传递给函数的参数转换为浮点数才能进行分割?这是必要的,还是他们更好的方式?
谢谢,
Scott Huey
I''m still pretty new to Python. I''m writing a function that accepts
thre integers as arguments. I need to divide the first integer by te
second integer, and get a float as a result. I don''t want the caller of
the function to have to pass floats instead of integers. How do I
convert the arguments passed to the function into floats before I do
the division? Is this necessary, or is their a better way?
Thanks,
Scott Huey
是的,这是必要的。如果你除以两个整数,结果将是一个
整数。
Yes, it is necessary. If you divide two integers, the result will be an
integer.
>> 1/2
>>1/2
0
你需要函数float()-float因为
整数和浮点数之间的划分将浮点数作为结果
0
You need the function float() -float because a division between
integers and floats will have floats as their results
>> float(1)/ 2
>>float(1)/2
0.5
0.5
Thomas Ploch写道:
Thomas Ploch wrote:
re **************** @ gmail.com schrieb:
re****************@gmail.com schrieb:
>我还是Python的新手。我正在编写一个接受
三元组作为参数的函数。我需要将第一个整数除以第二个整数,并得到一个浮点数作为结果。我不希望函数的调用者必须传递浮点数而不是整数。在我进行分割之前,如何将传递给函数的参数转换为浮点数?这是必要的,还是他们更好的方式?
谢谢,
Scott Huey
>I''m still pretty new to Python. I''m writing a function that accepts
thre integers as arguments. I need to divide the first integer by te
second integer, and get a float as a result. I don''t want the caller of
the function to have to pass floats instead of integers. How do I
convert the arguments passed to the function into floats before I do
the division? Is this necessary, or is their a better way?
Thanks,
Scott Huey
是的,它有必要的。如果你除以两个整数,结果将是一个
整数。
Yes, it is necessary. If you divide two integers, the result will be an
integer.
>> 1 / 2
>>1/2
0
你需要函数float()-float因为
整数和浮点数将有浮点数作为结果
0
You need the function float() -float because a division between
integers and floats will have floats as their results
>> float(1)/ 2
>>float(1)/2
0.5
0.5
>>来自__future__进口部门
1/2
>>from __future__ import division
1/2
0.5
-smithj
0.5
-smithj
2007-01-05,Jonathan Smith< sm **** @ rpath.comwrote:
On 2007-01-05, Jonathan Smith <sm****@rpath.comwrote:
> from __future__进口部门
1/2
>from __future__ import division
1/2
0.5
0.5
这篇关于除以整数...首先转换为浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!