Decimal part of a number in Python
I have the following program
def F_inf(a,b): x1=a.numerator/a.denominator x2=b.numerator/b.denominator if x1<x2: print "a<b" elif x1>x2: print "a>b" else: print "a=b" a=Fraction(10,4) b=Fraction(10,4) F_inf(a, b)
When I execute it,x1 receive just the integer value of the fraction, for exemple if I have to compute 2/4 x1 is equal to 0 not 0.5. What should I do ? Thanks
It sounds like you're using Python2. The best solution would be to switch to Python 3 (not just because of the division but because "Python 2.x is legacy, Python 3.x is the present and future of the language").
Other than that you have a couple of choices.
from __future__ import division # include ^ as the first line in your file to use float division by default
a = 1 b = 2 c = a / (1.0*b) # multiplying by 1.0 forces the right side of the division to be a float #c == 0.5 here