Floor value in math operators
Diez B. Roggisch
deets at nospam.web.de
Wed Apr 8 12:13:13 EDT 2009
Avi wrote:
> Hi,
>
> This will be a very simple question to ask all the awesome programmers
> here:
>
> How can I get answer in in decimals for such a math operator:
>
> 3/2
>
> I get 1. I want to get 1.5
You don't say which python-version you have. Depending on that, the answer
is different.
In python2.5 and before, dividing two integers will always return an
integer.
So for your problem, you need to do
3.0 / 2.0
At least one of the involved numbers must be a float.
However, from python2.5 the division operator behavior changed if desired -
if you do
>>> from __future__ import division
>>> 3 / 2
1.5
>>> 3 // 2
1
you get what you expect. The old division operator is still available, as //
In python2.6 and 3.x, the new behavior is standard.
Diez
More information about the Python-list
mailing list