本文介紹了將“十進制標記"千位分隔符添加到數字的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何在 Python 中將 1000000
格式化為 1.000.000
?'.' 在哪里是小數點千位分隔符.
How do I format 1000000
to 1.000.000
in Python? where the '.' is the decimal-mark thousands separator.
推薦答案
如果要添加千位分隔符,可以這樣寫:
If you want to add a thousands separator, you can write:
>>> '{0:,}'.format(1000000)
'1,000,000'
但它只適用于 Python 2.7 及更高版本.
But it only works in Python 2.7 and above.
請參閱格式字符串語法.
在舊版本中,您可以使用 locale.format():
In older versions, you can use locale.format():
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'en_AU.utf8'
>>> locale.format('%d', 1000000, 1)
'1,000,000'
使用 locale.format()
的額外好處是它將使用您的語言環境的千位分隔符,例如
the added benefit of using locale.format()
is that it will use your locale's thousands separator, e.g.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'de_DE.utf-8')
'de_DE.utf-8'
>>> locale.format('%d', 1000000, 1)
'1.000.000'
這篇關于將“十進制標記"千位分隔符添加到數字的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!