亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python?對數(shù)字的千分位處理方式

 更新時間:2022年05月17日 08:38:07   作者:guaguastd  
這篇文章主要介紹了Python?對數(shù)字的千分位處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

對數(shù)字的千分位處理

法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'

格式化千分位數(shù)字

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

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'
>>>?

正則實現(xiàn)

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)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論