how to make output 22//7
on python 2.7 become 3.14159
?i try use float(22/7)
but it just give me 3.0
. I try use Decimal
but it just give me 3
, use round(x, 6)
only give 3.0
just like float
.
Asked
Active
Viewed 960 times
0

Elonelon
- 68
- 1
- 12
-
try `float(22.0/7)` – Mahesh Karia Nov 15 '17 at 07:30
-
`float(22)/7`. One number needs to be of type float. `float(22/7)` fails because the result is already evaluated through integer division before casting the result to float. – roganjosh Nov 15 '17 at 07:30
-
Simply use 22./7, this will cast the result as a float – hcheung Nov 15 '17 at 07:32
-
1`from __future__ import division` then use `//` if you *want* integer division. – juanpa.arrivillaga Nov 15 '17 at 07:33
-
1I hope that no python version will give you a result of 3.14159 for 22/7, because that would be wrong. – jps Nov 15 '17 at 08:22
1 Answers
1
Here, int/int will return int only that is what happening here 22/7 gives 3 and you're type casting it to float(3) which is giving 3.0 but if you will perform float/int or int/float then it will result into float so you convert any of them to float as shown following.
replace float(22/7)
with float(22)/7

Mahesh Karia
- 2,045
- 1
- 12
- 23