Prashant Singh
Prashant Singh

Reputation: 316

Why doesn't decimal.getcontext().prec=3 work for decimal.Decimal(1.234)

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')

Upvotes: 0

Views: 130

Answers (1)

snakecharmerb
snakecharmerb

Reputation: 55864

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')

Upvotes: 1

Related Questions