日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python?對數字的千分位處理方式_python

作者:guaguastd ? 更新時間: 2022-07-09 編程語言

對數字的千分位處理

法1

>>> "{:,}".format(56381779049)
'56,381,779,049'
>>> "{:,}".format(56381779049.1)
'56,381,779,049.1'
>>>

法2

>>> import re
>>> subject = '1234567'
>>> result = re.sub(r"(?<=\d)(?=(?:\d\d\d)+$)", ",", subject)
>>> result
'1,234,567'

法3

>>> import re
>>> subject = '1234567'
>>> result = re.sub(r"(\d)(?=(\d\d\d)+(?!\d))", r"\1,", subject)
>>> result
'1,234,567'

格式化千分位數字

2.7版本以上直接用format設置千分位分隔符

Python 2.7 (r27:82500, Nov 23 2010, 18:07:12)
[GCC 4.1.2 20070115 (prerelease) (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> format(1234567890,',')
'1,234,567,890'
>>>?

正則實現

import re
def strConv(s): ?
? ? s = ?str(s)
? ? while True:
? ? ? ? (s,count) = re.subn(r"(\d)(\d{3})((:?,\d\d\d)*)$",r"\1,\2\3",s)
? ? ? ? if count == 0 : break
? ? return s
print strConv(12345)

原文鏈接:https://blog.csdn.net/guaguastd/article/details/42549267

欄目分類
最近更新