[Tutor] base n fractional
alan.gauld at btinternet.com
Mon Apr 6 01:25:57 CEST 2009
"Chris Castillo" <ctcast at gmail.com> wrote
>I need some help converting the fractional (right side of the decimal) to
> base 10.
> I have this but it doesn't work
Frankly, based on your algorithm, I'm not sure what exactly you want
to do but taking the above statement as a starting point
I'd convert the fractional part into an integer then divide by 10**(len(n))
n = raw_input('????') # say n ->1.234
d,f = n.split('.') # so d -> '1', f -> '234'
result = int(f)/(10**len(f)) # result = 234/10**3 = 234/1000 =
Or you could just add a dot back on and convert to float:
f = float('.'+f) # float('.234') -> 0.234
> for digit in range(len(myfrac) -1, -1, -1):
> mydecfrac = mydecfrac + int(myfrac[digit])
> mydecfrac = mydecfrac / base
This just confused me.
I think this might do what you are trying to do:
for digit in reversed(list('234')):
mydecfrac = (mydecfrac + int(digit)) / base
But that doesn't seem to do what I think you wanted?
Author of the Learn to Program web site
More information about the Tutor