How to break a fraction to fractional and whole number in Python

1 Answer

0 votes
import math     

for i in range(10):         
   print('{}/2 = {}'.format(i, math.modf(i / 2.0)))



'''
run:

0/2 = (0.0, 0.0)
1/2 = (0.5, 0.0)
2/2 = (0.0, 1.0)
3/2 = (0.5, 1.0)
4/2 = (0.0, 2.0)
5/2 = (0.5, 2.0)
6/2 = (0.0, 3.0)
7/2 = (0.5, 3.0)
8/2 = (0.0, 4.0)
9/2 = (0.5, 4.0)

'''

 



answered Jun 30, 2019 by avibootz
edited Jun 30, 2019 by avibootz
...