夜阑卧听风吹雨

铁马冰河入梦来

Loading

python 浮点数计算

Floating Point Math

先贴个代码,有空再写:

import decimal
import fractions

# 2017096.82 / 10000
print(f"2017096.82 / 10000 => {2017096.82 / 10000}")

ori = fractions.Fraction(decimal.Decimal('2017096.82'))
denominator = fractions.Fraction(10000)
print(f"fractions 2017096.82 / 10000 => {float(ori / denominator)}")

print("=" * 45)

# 266722.2 / 10000
print(f"266722.2 / 10000 => {266722.2 / 10000}")

bid_amount = float(266722.2)
ori = fractions.Fraction(decimal.Decimal(bid_amount))
denominator = fractions.Fraction(10000)

print(f"fractions 266722.2 / 10000 => {float(ori / denominator)}")
print((ori / denominator))
print(916450500893737 / 34359738368000)

print("=" * 45)

# decimal
print(decimal.Decimal(float("266722.2")))
print(decimal.Decimal(266722.2))
print(decimal.Decimal("266722.2"))

print("-" * 45)

print(str(decimal.Decimal("266722.2") / 10000))
print(str(decimal.Decimal("2017096.82") / 10000))

输出:

2017096.82 / 10000 => 201.70968200000002
fractions 2017096.82 / 10000 => 201.709682
=============================================
266722.2 / 10000 => 26.672220000000003
fractions 266722.2 / 10000 => 26.672220000000003
916450500893737/34359738368000
26.672220000000003
=============================================
266722.2000000000116415321826934814453125
266722.2000000000116415321826934814453125
266722.2
---------------------------------------------
26.67222
201.709682
posted @ 2022-09-30 09:47  二次蓝  阅读(130)  评论(0编辑  收藏  举报