2015-11-29 34 views

回答

6

只有一個可用的千位分隔符。

','選項表示使用千位分隔符的逗號。

docs

例子:

'{:,}'.format(x) # 4,100,200,300 

如果你需要使用一個點作爲千個分隔符,考慮'.'更換逗號或設置地方(LC_NUMERIC類)適當。

您可以使用this列表來查找正確的語言環境。請注意,您必須使用n整數顯示類型用於區域識別的格式:

import locale 
locale.setlocale(locale.LC_NUMERIC, 'de_DE') # or da_DK, or lt_LT, or mn_MN, or ... 
'{:n}'.format(x) # 4.100.200.300 

在我看來,前一種方法則簡單得多:

'{:,}'.format(x).replace(',', '.') # 4.100.200.300 

format(x, ',').replace(',', '.') # 4.100.200.300