Decimals to fraction strings

Mark Jackson mjackson at wc.eso.mc.xerox.com
Tue May 16 14:31:43 EDT 2000


=?ISO-8859-1?Q?Fran=E7ois_Pinard?= <pinard at iro.umontreal.ca> writes:
> "Stuart D. Gathman" <stuart at bmsi.com> writes:
> 
> > Scott wrote:
> 
> > > Does anyone know of a way to convert decimal numbers to a string
> > > representation of the fractional value? For example:
> > > 0.5 = "1/2"
> > > or
> > > 1.125 = "1 1/8"
> 
> > 1) Convert to a fraction by counting places after the decimal:
> > 2) Divide top and bottom by their GCD (greatest common divisor):
> > 3) If desired (I don't), convert to mixed notation when num > den by
> > finding quotient and remainder of num / den:
> 
> Yet your suggestion is straightforward, it might not always yield the "best"
> answer, because of the constraint put on the denominator to initially be
> an exponent of 10.

<dumb look>
Is not this same constraint applied by the problem itself, which starts
with a [finite length] decimal representation?
</dumb look>

-- 
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
	In judging others, folks will work overtime for no pay.
				- Charles Carruthers





More information about the Python-list mailing list