views:

130

answers:

5

So if i run

a = b / c

and get the result 1.2234

How do i separate it so that I have

a = 1 b = 0.2234

+1  A: 
b = a % 1
a = int(a)

or something

Tor Valamo
For `a = -1.7` the result is `-2` and `0.3`, which is probably not what the author wanted to get.
Bolo
A: 
int(a)/b == 1
(a/b)%1 == 0.2234
Nicolas78
+6  A: 
a,b = divmod(a, 1)
gnibbler
For `a = -1.7` the result is `-2` and `0.3`, which is probably not what the author wanted to get.
Bolo
beautiful thanks
joe schmoe
i wont have negative numbers involved.
Incognito
+5  A: 

Try:

a, b = int(a), a - int(a)

Bonus: works for negative numbers as well. -1.7 is split into -1 and -0.7 instead of -2 and 0.3.

EDIT If a is guaranteed to be non-negative, then gnibbler's solution is the way to go.

EDIT 2 IMHO, Odomontois' solution beats both mine and gnibbler's.

Bolo
+6  A: 
>>> from math import modf
>>> b,a = modf(1.2234)
>>> print ('a = %f and b = %f'%(a,b))
a = 1.000000 and b = 0.223400
>>> b,a = modf(-1.2234)
>>> print ('a = %f and b = %f'%(a,b))
a = -1.000000 and b = -0.223400
Odomontois