# xlabel - subplot title python

## 我怎樣才能迫使部門成為浮點? 司保持四捨五入到0 (9)

``````c = a / b
``````

# 我如何強制部門成為Python中的浮點？

``````c = a / b
``````

“我如何強制真正的分裂，使`a / b`將返回一小部分？”

## 升級到Python 3

``````>>> 1/2
0.5
``````

``````>>> 1//2
0
>>> 1//2.0
0.0
``````

## 如果使用Python 2

### 推薦Python 2

``````from __future__ import division
``````

``````>>> from __future__ import division
>>> 1/2
0.5
>>> 1//2
0
>>> 1//2.0
0.0
``````

### Python 2的其他選項

``````>>> 1/(2 * 1.0)
0.5
``````

`truediv`來自`operator.truediv(a, b)` `truediv` ，但是這可能會比較慢，因為它是一個函數調用：

``````>>> from operator import truediv
>>> truediv(1, 2)
0.5
``````

### 不推薦用於Python 2

``````>>> 1 / float(2)
0.5
>>> 1 / float(2j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float
``````

``````\$ python -Qnew -c 'print 1/2'
0.5
\$ python -Qnew -c 'print 1/2j'
-0.5j
``````

``````0.2
``````

``````aa = 2
bb = 3

aa / bb = 0

1. * aa / bb = 0.6666666666666666
``````

;-)

Max - 意大利

``````from __future__ import division
``````

``````c = float(a)/b
``````

``````c = a/float(b)
``````

``````>>> 4/3.
1.3333333333333333
``````

``````>>> from __future__ import division
>>> 4/3
1.3333333333333333
``````

``````>>> from __future__ import division
>>> a = 4
>>> b = 6
>>> c = a / b
>>> c
0.66666666666666663
``````

``````c = a / (b * 1.0)
``````

``````from operator import truediv

c = truediv(a, b)
``````

a是分紅，b是除數。 當兩個整數相除後的商是浮點數時，該函數非常方便。