Python模块之: fractions模块-分数模块

fractions 模块可以被用来执行包含分数的数学运算。

>>> from fractions import Fraction
>>> a = Fraction(5, 4)
>>> b = Fraction(7, 16)
>>> print(a + b)
27/16
>>> print(a * b)
35/64
>>> # Getting numerator/denominator
>>> c = a * b
>>> c.numerator
35
>>> c.denominator
64
>>> # Converting to a float
>>> float(c)
0.546875

 

posted @ 2019-07-26 10:53  超越吧  阅读(905)  评论(0编辑  收藏  举报