1
from decimal import Decimal
Decimal(6.49)

Decimal('6.4900000000000002131628207280300557613372802734375')

Decimal("6.49")

Decimal('6.49')

Umm, why would these be different? Is this a bug in the Decimal library? Or is this indeed desired behavior? If so, why?

Chase Roberts
  • 9,082
  • 13
  • 73
  • 131
  • 1
    6.4900000000000002131628207280300557613372802734375 is the exact value of the float you used. – user2357112 Jul 11 '18 at 02:30
  • 1
    I understand how floating point numbers work, and when I asked the question it wasn't obvious to me what the problem was. But coming back to it today, it's now obvious to me. `Decimal(6.49)` is creating a decimal out of the float number `6.490000...213...`, whereas `Decimal('6.49')` is creating a decimal straight from the string, without converting it to a float first. – Chase Roberts Jul 12 '18 at 21:46

0 Answers0