您现在的位置是:网站首页> 编程资料编程资料
Python 对数字的千分位处理方式_python_
2023-05-26
760人已围观
简介 Python 对数字的千分位处理方式_python_
对数字的千分位处理
法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)以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
相关内容
- python协程与 asyncio 库详情_python_
- Python 操作 Excel 之 openpyxl 模块_python_
- python处理excel文件之xlsxwriter 模块_python_
- Python实现功能全面的学生管理系统_python_
- Python如何通过变量ID得到变量的值_python_
- python实现简易的学生信息管理系统_python_
- Python中typing模块的具体使用_python_
- python实现简单通讯录管理系统_python_
- Tensorflow中使用cpu和gpu有什么区别_python_
- Pytorch从0实现Transformer的实践_python_
