[Tutor] Decimal module question

Kent Johnson kent37 at tds.net
Wed Dec 6 14:35:00 CET 2006


Dick Moores wrote:
> I wrote this function:
> 
> def numberRounding(n, significantDigits=4):
> 	"""
> 	Rounds a number (float or integer, negative or positive) to any number of
> 	significant digits. If an integer, there is no limitation on it's size.
> 	"""
> 	import decimal
> 	def d(x):
> 		return decimal.Decimal(str(x))
> 	decimal.getcontext().prec = significantDigits
> 	return d(n)/1
> 
> If the last line is written as the more normal-looking "return d(n)", 
> it won't work. Why?

The context precision is applied to *operations* on decimals, not to 
construction.

The docs for the constructor Decimal([value [, context]]) say, "The 
context precision does not affect how many digits are stored. That is 
determined exclusively by the number of digits in value. For example, 
"Decimal("3.00000")" records all five zeroes even if the context 
precision is only three."

The docs for Context say, "The prec field is a positive integer that 
sets the precision for *arithmetic operations* in the context." (my 
emphasis)

So you have to perform an operation for the precision to have any 
effect. Dividing by 1 is an operation.

Kent



More information about the Tutor mailing list