How to expand numbers

I have the number 4.476 for example and I’d like to expand that like 4 + 4/10 + 7/100 + 6/1000. There is my code below

t = []
count = 0
dec = str(n).split('.')
for i in dec[0]:
    t.append(i)
for j in dec[1]:
    t.append(j + "/" + str(10 ** (len(dec[1])-count-1)))
    count += 1

print(' + '.join(t))

But that doesn’t work at all

1 Like

Hello @fullbringcoder.

Where do you think the problem lies? And how did you try to solve it?

I have the number 4.476 for example and I’d like to expand that like 4 + 4/10 + 7/100 + 6/1000 .

Base on your code, when n = 4.476, your current result: 4 + 4/100 + 7/10 + 6/1
The problem is in : str(10 ** (len(dec[1])-count-1)))
If you print out the calculation for “to the power”, you should be able to see the problem print(len(dec[1]) - count - 1)
In reality what you want is very simple, increasing with count, each time.

1 Like

OK I see. Thanks for the reply