Skip to content Skip to sidebar Skip to footer

How To Create A Decimal.decimal Object With A Given Number Of Significant Figures?

The best way I've found to produce a decimal.Decimal number with a specific number of significant figures is the one used to initialize the variable kluge below: import decimal THI

Solution 1:

You seem to be looking for Context.create_decimal:

Creates a new Decimal instance from num but using self as context. Unlike the Decimal constructor, the context precision, rounding method, flags, and traps are applied to the conversion.

>>> decimal.Context(prec=5).create_decimal(0.00001/3.0)
Decimal('0.0000033333')

Post a Comment for "How To Create A Decimal.decimal Object With A Given Number Of Significant Figures?"