0

Why doesn't decimal.getcontext().prec for decimal.Decimal(x)? It gives a long decimal value:

>>>decimal.getcontext().prec = 3

>>>decimal.Decimal(2.345)
Decimal('2.345000000000000195399252334027551114559173583984375')
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Prashant Singh
  • 156
  • 1
  • 5
  • Take a moment to read through the [editing help](//stackoverflow.com/editing-help) in the help center. Formatting on Stack Overflow is different than on other sites. The better your post looks, the easier it is for others to read and understand it. – Dharman Nov 09 '19 at 17:10

1 Answers1

1

As documented in the tutorial,

The significance of a new Decimal is determined solely by the number of digits input. Context precision and rounding only come into play during arithmetic operations.

>>> decimal.getcontext().prec = 3
>>> decimal.Decimal(2.345)
Decimal('2.345000000000000195399252334027551114559173583984375')
>>> decimal.Decimal(2.345) + decimal.Decimal(4)
Decimal('6.35')
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153