python 浮点数计算
先贴个代码,有空再写:
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