In python, how to break a number into a decimal point

So, if I run:

a = b / c 

and get the result 1.2234

How to separate it so that I have:

 a = 1 b = 0.2234 
+14
python decimal floating-point split
Aug 10 '10 at
source share
6 answers
 >>> 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 
+16
Aug 11 2018-10-10T00:
source share
 a,b = divmod(a, 1) 
+9
Aug 10 '10 at 22:52
source share

Try:

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

Bonus: works for negative numbers too. -1.7 is divided by -1 and -0.7 instead of -2 and 0.3 .

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

EDIT 2 IMHO, the Odomontois solution hits both mine and gnibblers.

+4
Aug 10 2018-10-10T00:
source share
 b = a % 1 a = int(a) 

or something

+1
Aug 10 2018-10-10T00:
source share
 int(a)/b == 1 (a/b)%1 == 0.2234 
0
Aug 10 2018-10-10T00:
source share
 x = 1.2234 y = str(x/100).split('.') a = y[0] b = y[1] 



then the result ...

 a = 1 b = 2234 
0
Apr 30 '15 at 7:55
source share



All Articles