2025-09-10:仓库迁移
This commit is contained in:
24
3.数字日期和时间/8.分数的计算.py
Normal file
24
3.数字日期和时间/8.分数的计算.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from fractions import Fraction
|
||||
|
||||
from unicodedata import decimal
|
||||
from xlrd.formula import oMSNG
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 如果遇到分数问题,那么我们就需要使用fractions模块
|
||||
a = Fraction(1, 2)
|
||||
print("这是二分之一:{}".format(a))
|
||||
|
||||
# 同样的,分数可以进行所有的算术运算
|
||||
print(a * a)
|
||||
|
||||
# 我们可以通过一些方法得到分子和分母
|
||||
print("分子是:{}".format(a.numerator))
|
||||
print("分母是:{}".format(a.denominator))
|
||||
|
||||
# 或者我们可以把分数转成浮点数
|
||||
print(float(a))
|
||||
|
||||
# 下面展示如何正确计算高精度的pi/8
|
||||
import numpy as np
|
||||
from decimal import Decimal
|
||||
print(np.pi * Fraction(1, 8))
|
Reference in New Issue
Block a user