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

Python 对数字的千分位处理

发布时间:2019-09-05 07:01:30编辑:auto阅读(3140)

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


关键字