Carrie
Carrie

Reputation: 31

Making a decimal a whole number

I need to take a number like (0.25) and multiply by ten repeatedly until there are no values after the decimal point.

For example, if the number was .25 then I would need to multiply by ten repeatedly:

.25 * 10 * 10 = 25

or if the number was 1.5

1.5 * 10 = 15

What would be the best way to do this? A for loop? I know I should do something like

number = .75
for i in range(0,10):
    number * 10**i = ???? (this is where I am stuck)

Upvotes: 2

Views: 159

Answers (3)

w33haa
w33haa

Reputation: 314

Another way

x=.25
while x % 1.0 != 0:
    x=x*10

Upvotes: 0

Chun-Yen Wang
Chun-Yen Wang

Reputation: 578

If the input is always positive fraction (1 > x > 0), I would try this:

def fraction2int(b):
    return int('{}'.format(b).replace('0.', ''))

Upvotes: 0

SuperStew
SuperStew

Reputation: 3054

You could do this

x=.25
while int(x) != x:
    x=x*10

Upvotes: 2

Related Questions