[Tutor] How to convert a long decimal into a string?

Terry Carroll carroll at tjc.com
Tue Jan 16 21:40:43 CET 2007


On Tue, 16 Jan 2007, Dick Moores wrote:

> Here's a function I wrote some time ago, and just discovered that in 
> one important category of cases, long numbers with a decimal point, 
> it doesn't do what I intended.
> 
> =====================================================
> 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
> ======================================================
> 
> Now, print 
> numberRounding(232.3452345230987987098709879087098709870987098745234, 
> 30) prints
> 232.345234523
> 
> whereas if the first argument is enclosed in quotes, it does what I 
> indended. Thus:
> print 
> numberRounding('232.3452345230987987098709879087098709870987098745234', 
> 30) prints
> 232.345234523098798709870987909 .
> 
> So my question is, how can I revise numberRounding() so that it is 
> not necessary to employ the quotes.

I'm guessing that you can't.

When you do:

numberRounding(232.3452345230987987098709879087098709870987098745234,30)

you are calling numberRounding with a first argument of a floating point,
whose value is set from
232.3452345230987987098709879087098709870987098745234, but is going to be 
cut down immediately to the amount of precision that you have on your 
machine.  For me, that's 232.34523452309881, well short of 30.

So it's the same as calling:

numberRounding(232.34523452309881,30)

By the time your function gets control, you're already down to the 
less-precise value.

That's probably why a direct decimal conversion from float is not allowed,
to avoid the illusion of more precision than there really is.




More information about the Tutor mailing list