Converting an improper fraction to a mixed number with python

前端 未结 3 431
萌比男神i
萌比男神i 2021-01-22 21:40

I need to us python to convert an improper fraction to a mixed number or even a float to a mixed number. My code is as follows:

from fractions import Fraction
nu         


        
3条回答
  •  自闭症患者
    2021-01-22 22:13

    Try to implement it manually:

      a = numerator // denominator 
      b = numerator % denominator
      print '{} {}/{}'.format(a, b, denominator)
    

    Currently you are just printing 'x/b', if the input is a float do the adequate translation first.

    Hope it can help.

提交回复
热议问题