Dividing integers...Convert to float first?
Jonathan Smith
smithj at rpath.com
Fri Jan 5 18:35:51 CET 2007
Thomas Ploch wrote:
> redefined.horizons at gmail.com schrieb:
>> 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
> 0
>
> You need the function float() -> float because a division between
> integers and floats will have floats as their results
>
> >>> float(1)/2
> 0.5
>>> from __future__ import division
>>> 1/2
0.5
-smithj
More information about the Python-list
mailing list