2010-08-10 23 views

回答

1
b = a % 1 
a = int(a) 

什麼

+1

對於'A = -1.7'結果是'-2'和'0.3',這可能不是什麼作者想要得到。 – Bolo 2010-08-10 23:01:46

9
a,b = divmod(a, 1) 
+1

對於'a = -1.7',結果是'-2'和'0.3',這可能不是作者想要得到的。 – Bolo 2010-08-10 23:01:09

+0

美麗的感謝 – 2010-08-10 23:02:26

+0

我不會有負號涉及。 – Incognito 2010-08-10 23:03:50

4

嘗試:

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

獎勵:適用於負數爲好。 -1.7拆分爲-1-0.7而不是-20.3

編輯如果a保證是非負的,那麼gnibbler的解決方案就是要走的路。

編輯2恕我直言,Odomontois的解決方案擊敗了我的和gnibbler的。

16
>>> 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 
0
x = 1.2234 

y = str(x/100).split('.') 

a = y[0] 
b = y[1] 

那麼結果是...

a = 1 
b = 2234 
+0

b = 2234不是OP希望的。他想要b = 0.2234 – zelite 2015-04-30 08:14:50