well i want to use a for loop to work from the right most digit to the left most digit at a negative 1 increment. For each digit i want to divide it by 1/base and add the next digit. Then I need to take that sum and multiply it by 1/b to get the decimal equivalent
so when I say that to myself i see: number = 234 mysum = 0 for digit in range(len(number) -1, -1, -1): mysum = (mysum) * (1/base) + int(number[digit]) On Sun, Apr 5, 2009 at 6:25 PM, Alan Gauld <alan.ga...@btinternet.com>wrote: > > "Chris Castillo" <ctc...@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)) > > Something like: > > 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 = > 0.234 > > 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? > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/l2p/ > > _______________________________________________ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor >
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor