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

Kent Johnson kent37 at tds.net
Tue Jan 16 21:16:07 CET 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

The problem is that 
232.3452345230987987098709879087098709870987098745234 is a float which 
cannot represent this number exactly. Just typing it at the interpreter 
prompt shows the problem:
 >>> 232.3452345230987987098709879087098709870987098745234
232.34523452309881
 >>> str(_)
'232.345234523'

So the precision you want is lost immediately when the constant is created.
> 
> 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. 

You can't. A float simply can't represent the number you want and the 
function has no way to access the textual representation of the number.

 > Or alternatively, is there a way
> to non-manually put quotes around an argument that is a long decimal? 
No

Kent



More information about the Tutor mailing list