Files
Python_CookBook_repo/3.数字日期和时间/2.执行精确小数计算.py
2025-09-10 16:12:45 +08:00

13 lines
343 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from decimal import Decimal
if __name__ == '__main__':
# 可以看到使用float的ieee754浮点标准进行计算会产生误差
a = 4.2
b = 2.1
print(a+b)
# 如果需要高精度小数我们的处理方法是将数字字符串转成decimal类型
a = Decimal(str(4.2))
b = Decimal(str(2.1))
print(a+b)