-
2020-11-24 12:31:38
@ user136036的答案是相当不错的,但是不幸的是,它没有考虑到Python错误的真实性。 完整答案可能如下:
变体A
如果您的平台的语言环境运行正常,则只需使用语言环境即可:
import locale
locale.setlocale(locale.LC_ALL, '')
print("{:,d}".format(7123001))
结果取决于您的语言环境和Python实现方式。
但是如果根据区域设置的Python格式被破坏,例如 Linux上的Python 3.5?
变体B
如果Python不遵守print("{:,.2f}".format(7123001.345).replace(",", " ")参数,则可以使用区域设置和解决方法(使用货币格式):
locale.setlocale(locale.LC_ALL, '')
locale._override_localeconv = {'mon_thousands_sep': '.'}
print(locale.format('%.2f', 12345.678, grouping=True, monetary=True))
以上在我的平台上给出了12.345,68。 将money设置为False或忽略它-Python不会分组成千上万。指定print("{:,.2f}".format(7123001.345).replace(",", " ")不执行任何操作。
变体C
如果您没有时间检查平台上的Python是否正常运行以及哪些功能坏了,则可以使用常规的字符串替换功能(如果您想将逗号和点换成点和逗号):
print("{:,.2f}".format(7123001.345).replace(",", " ")
用逗号代替空格很简单(点被假定为小数点分隔符):
print("{:,.2f}".format(7123001.345).replace(",", " ")
更多相关内容 -
如何在python中将千位分隔符添加到已转换为字符串的数字中?
2020-12-11 06:02:49它使用了最广泛使用的千位分隔符(,)和十进制标记(.),但它可以很快地修改为与其他区域设置的符号一起使用,或者用于创建一个适用于所有区域设置的解决方案。在def separate_thousands_with_delimiter(num_str): ...这个问题我来晚了,我在寻找解决办法的时候发现了这个问题。在
将,符号与format()一起使用可以很好地工作,但是会带来一些问题,因为不幸的是,,符号不能应用于字符串。因此,如果您从数字的文本表示开始,那么在调用format()之前,必须将它们转换为整数或浮点。如果您需要同时处理需要保留的不同精度级别的整数和浮点数,format()代码会迅速变得相当复杂。为了处理这种情况,我最终编写了自己的代码,而不是使用format()。它使用了最广泛使用的千位分隔符(,)和十进制标记(.),但它可以很快地修改为与其他区域设置的符号一起使用,或者用于创建一个适用于所有区域设置的解决方案。在def separate_thousands_with_delimiter(num_str):
"""
Returns a modified version of "num_str" with thousand separators added.
e.g. "1000000" > "1,000,000", "1234567.1234567" > "1,234,567.1234567".
Numbers which require no thousand separators will be returned unchanged.
e.g. "123" > "123", "0.12345" > "0.12345", ".12345" > ".12345".
Signed numbers (a + or - prefix) will be returned with the sign intact.
e.g. "-12345" > "-12,345", "+123" > "+123", "-0.1234" > "-0.1234".
"""
decimal_mark = "."
thousands_delimiter = ","
sign = ""
fraction = ""
# If num_str is signed, store the sign and remove it.
if num_str[0] == "+" or num_str[0] == "-":
sign = num_str[0]
num_str = num_str[1:]
# If num_str has a decimal mark, store the fraction and remove it.
# Note that find() will return -1 if the substring is not found.
dec_mark_pos = num_str.find(decimal_mark)
if dec_mark_pos >= 0:
fraction = num_str[dec_mark_pos:]
num_str = num_str[:dec_mark_pos]
# Work backwards through num_str inserting a separator after every 3rd digit.
i = len(num_str) - 3
while i > 0:
num_str = num_str[:i] + thousands_delimiter + num_str[i:]
i -= 3
# Build and return the final string.
return sign + num_str + fraction
# Test with:
test_nums = ["1", "10", "100", "1000", "10000", "100000", "1000000",
"-1", "+10", "-100", "+1000", "-10000", "+100000", "-1000000",
"1.0", "10.0", "100.0", "1000.0", "10000.0", "100000.0",
"1000000.0", "1.123456", "10.123456", "100.123456", "1000.123456",
"10000.123456", "100000.123456", "1000000.123456", "+1.123456",
"-10.123456", "+100.123456", "-1000.123456", "+10000.123456",
"-100000.123456", "+1000000.123456", "1234567890123456789",
"1234567890123456789.1", "-1234567890123456789.1",
"1234567890123456789.123456789", "0.1", "0.12", "0.123", "0.1234",
"-0.1", "+0.12", "-0.123", "+0.1234", ".1", ".12", ".123",
".1234", "-.1", "+.12", "-.123", "+.1234"]
for num in test_nums:
print("%s > %s" % (num, separate_thousands_with_delimiter(num)))
# Beginners should note that an integer or float can be converted to a string
# very easily by simply using: str(int_or_float)
test_int = 1000000
test_int_str = str(test_int)
print("%d > %s" % (test_int, separate_thousands_with_delimiter(test_int_str)))
test_float = 1000000.1234567
test_float_str = str(test_float)
print("%f > %s" % (test_float, separate_thousands_with_delimiter(test_float_str)))
希望这有帮助。:)
-
python – 如何设置自定义千位分隔符?
2020-12-11 06:02:55我知道理论上大整数的数字可以按数千个分组,以提高可读性:Python 3.5.2 (default, Nov 17 2016, 17:05:23)[GCC 5.4.0 20160609] on linuxType "help", "copyright", "credits" or "license" for more information.&...我知道理论上大整数的数字可以按数千个分组,以提高可读性:
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
'en_US.UTF-8'
>>> locale.format('%d', 1234567890, grouping=True)
'1,234,567,890'
>>> "{:n}".format(1234567890)
'1,234,567,890'
但是,令人惊讶的是,这不适用于每个语言环境:
>>> locale.setlocale(locale.LC_ALL, 'pl_PL.UTF-8')
'pl_PL.UTF-8'
>>> locale.format('%d', 1234567890, grouping=True)
'1234567890'
>>> "{:n}".format(1234567890)
'1234567890'
为什么数字没有格式化?我觉得这很奇怪.我希望打印出类似于1 234 567 890的东西.
根据Format Specification Mini-Language,我们可以明确地强制执行两个可能的分隔符:逗号和下划线_.可悲的是,逗号不适合波兰语,因为它在那里被用作小数点分隔符,而像1_234_567_890这样的数字对于大多数人来说看起来很奇怪.
我们能否以某种方式强制使用不间断的空间作为千位分隔符?
解决方法:
pl_PL语言环境千位分隔符似乎是empty.我不知道这是否准确地表示波兰的常见用法,但Python正确地根据pl_PL语言环境的规则格式化您的数字.这可能是区域设置文件中的错误.
据我所知,没有选项可以手动指定千位分隔符和十进制标记字符.
标签:python,integer,formatting,locale
来源: https://codeday.me/bug/20190701/1350697.html
-
python 千位分隔符,
2020-11-21 01:37:07相关推荐2019-09-28 21:13 −Python python是一种跨平台的计算机程序设计语言,是一种面向对象的动态类型语言。 最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的...原博文
2019-01-07 13:06 −
>>>'{:,}'.format(1234567890) >>>'1,234,567,890' ...
相关推荐
2019-09-28 21:13 −
Python python是一种跨平台的计算机程序设计语言,是一种面向对象的动态类型语言。 最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发。(以上摘自百度百科) Python是一种解释型脚本语言,可以应用于以下领域...
0
1961
2019-12-24 19:55 −
1 Web应用 https://www.cnblogs.com/yuanchenqi/articles/8869302.html2 http协议 https://www.cnblogs.com/yuanchenqi/articles/8875623...
2019-12-04 22:39 −
是不是看到题目Python就点进来了。
其实,只是个蒟蒻......
不知道学校发了什么疯,现在连普通的信息课都在讲Python,是怕我们消化不了c++吗?
虽然心里很不甘心,但不得不承认,许多时候(实际上是大部分),Python都比c++更简单。
原因:
1.Python不用打头文件(咳咳,纯属个...
0
465
2019-12-06 14:02 −
学习提纲
生成指定范围的数值列表,语法格式如下:
生成一个包括10哥随机数的列表,要求数的范围在10-100之间,代码如下
import random
randomnumber = [random.randint(10,100) for i in range(...
2019-12-18 15:00 −
保留字:and,as,assert,break,class,continue,
def,del,elif,else,except,finally,
forfromFalse,global,if,import,
in,is,lambda,nonlocal,not...
0
181
2019-12-24 21:14 −
python-requests
python-requests
作用:能够模拟浏览器向服务器发送请求的库
requests库的主要方法
方法描述
requests.request()构造一个请求,支持以下各种方法requests.get()获取html的主要方法requests.head(...
0
524
2019-12-11 17:16 −
Redis
redis是一个key-value存储系统。
赋值:set name alex查看所有key:keys *查看key对应的value:get name只存活2秒钟:set name jack ex 2
Python操作Redissudo pip install redis
1、操作模式r...
2019-11-27 16:55 −
1.安装
需要安装得模块名为pyyaml,直接pip install pyyaml
导入,直接import yaml
2.yaml文件的格式
文件格式输出可以是列表,可以是字典,可以嵌套。层级关系用空格区分,切记,不支持tab缩进
a)键值对形式
user: admin
pwd: 123
...
2019-11-23 12:52 −
# Python3随手记 - [Python3随手记](#python3%e9%9a%8f%e6%89%8b%e8%ae%b0) - [list方法](#list%e6%96%b9%e6%b3%95) - [os](#os) - [imageio](#imageio) - [Python I...
-
python – 为pandas数据帧中的整数设置千位分隔符
2020-11-24 12:31:38格式(数字),如下例所示,格式化pandas数据帧中的数字:# This works for floats and integersprint '{:,}'.format(20000)# 20,000print '{:,}'.format(20000.0)# 20,000.0问题是,对于具有整数的数据帧不起作用,并且在... -
python - 如何使用逗号作为千位分隔符打印数字?
2020-11-24 12:31:40我正在使用python 2.5,因此我无法访问内置格式。我查看了Django代码intcomma(下面的代码中的intcomma_recurs),并意识到它效率低下,因为它是递归的,并且在每次运行时编译正则表达式也不是一件好事。 这不是一个... -
31 python中format方法:字段宽度、精度和千位分隔符 符号、对齐和用0填充
2020-11-24 12:31:39第六课 字段宽度、精度和千位分隔符(format方法)# 字段宽度、精度和千位分隔符# 100,000,000,000# 让一个数值在宽度为2的范围内输出,如果数值没到12位,左侧填充空格 4位呢print("a:{num:2}".format(num = 32)) # a... -
Python基础:增加和去除数字的千位分隔符
2021-10-10 15:19:49千位分隔符,其实就是数字中的逗号。依西方的习惯,人们在数字中加进一个符号,以免因数字位数太多而难以看出它的值。所以人们在数字中,每隔三位数加进一个逗号,也就是千位分隔符,以便更加容易认出数值。 处理... -
如何设置自定义千位分隔符?
2021-07-16 14:40:06I know that theoretically digits in large integers can be grouped by thousands for better readability:Python 3.5.2 (default, Nov 17 2016, 17:05:23)[GCC 5.4.0 20160609] on linuxType "help", "copyright... -
千位分隔符的完整攻略
2020-12-11 06:04:16千位分隔符纯整数情况纯整数大概是所有情况里最简单的一种,我们只要正确匹配出千分位就好了。观察上面的数字,我们可以得出千分位的特征是到字符串终止位有 3n 个数字,不包括起始位。于是可以得到这样的函数:... -
玩转千位分隔符输出 - leejun2005的个人页面 - OSCHINA - 中文开源技术交流社区
2021-01-12 23:12:491、Python1.1 format方法:2.7版本以上直接用format设置千分位分隔符Python2.7(r27:82500,Nov232010,18:07:12)[GCC4.1.220070115(prerelease)(SUSELinux)]onlinux2Type"help","copyright","credits"or"license"form.... -
Python-字符串的格式化、对齐、符号选项、千位分隔符、精度、输出类型、f-字符串
2022-02-12 12:47:20‘字{}符串’.format(变量)用变量替换{}的值 {下标}想{小标}'.format('元素1', '元素2'){}里的下标是将要填入的元素的下标,一个元素也可以被多次填入到花括号中 '{name},{thing}'.format(name='xx', thing='xx')也... -
python中怎么把千位分隔符以及货币符号去掉转成数值形式?
2019-07-10 21:22:12python中怎么把千位分隔符以及货币符号去掉转成数值形式? 比如下面的这种 $10,000 ¥1,000,000.00 怎么转换成数值? 用正则表达就可以了 from re import sub money = '¥1,000,000' val = float(sub(r'[^... -
在pandas数据框架中格式化整数的千位分隔符
2020-11-24 12:31:35return fmt_values 我假设您真正需要的是如何重写所有整数的格式:replace(“monkey patch”)theIntArrayFormatter以打印由逗号分隔的数千个整数值,如下所示:import pandas class _IntArrayFormatter(pandas.io... -
使用千位分隔符和字体大小格式化y轴matplotlib
2020-11-24 12:31:34labelsize=5000) 以下是您如何更改代码以合并这些解决方案(根据注释中的要求):%matplotlib inline import matplotlib.pyplot as plt plt.style.use('seaborn-white') import numpy as np from numpy import ... -
python中将千位分隔符的数字转化为常规数值
2017-01-12 18:19:00将 “12,345.678” (str) 转化为 12345.678 (float) fromlocaleimport* setlocale(LC_NUMERIC,'English_US') atof('123,456')# 123456.0 转自... -
Python设置数字格式:小数位数、百分号、千位分隔符
2021-05-04 16:34:00设置千位分隔符:自定义函数+格式化处理 df['data'].map(lambda x : format(x, ',')) # 处理后依然是对象格式。设置千位分割符请小心操作,因为对电脑来说,这些已经不再是数字了,而是数字和逗号组成的字符串,要... -
js为数字添加千位分隔符
2020-11-24 12:31:04使用一条正则表达式将12345678转为12,345,678看到一个方法"12345678".replace(/(\d)(?=(?:d{3})+$)/g, '$1,')但是不太明白怎么实现的,或者大神们有没有更好的方法多谢各位的解答,说说自己的理解:主要是要搞懂?... -
含千位分隔符的正整数字符串转整型输出
2022-04-15 18:52:16输入一个包含千位分隔符(英文逗号)的正整数字符串,输出不带千分符的正整数。如果不能转换为整数或千分符格式不正确就输出“数据错误”。例如,输入的字符串为'1,234'输出1234;输入'1s3'或'12,34'输出“数据错误... -
python处理千分位分隔符有逗号,点号,空格等情况
2020-11-21 01:37:06radix_point_left_str = split_result[0] # 小数点右边 radix_point_right_str = "" if 1 (split_result): radix_point_right_str = save_decimal_place_str.split('.')[1] # 千分位的分隔符以逗号作为基准 ... -
Python用逗号千位分隔符替换中间数字
2020-12-08 14:23:18我有一个像这样的字符串:123456789....然后使用中间的数字组(我需要保留),我需要放置一个逗号数千个分隔符.所以这里的输出是:123,456,789我可以使用lookarounds捕获中间的数字,但它不会取代其他数字,我不知道如... -
金额千位分隔符及保留2位小数
2021-12-06 16:49:13JS 实现千万分隔符,toFixed() 返回四舍五入指定小数位数 -
Python控制千分位分隔符格式
2021-03-24 17:36:56print("{:,.2f}".format(111115346.28)) -
python格式带空格的字符串千分隔符
2021-03-07 01:05:50@user136036的答案很好,但不幸的是,它没有考虑到Python错误的实际情况。完整答案如下:变体A如果平台的区域设置工作正常,则只需使用区域设置:import localelocale.setlocale(locale.LC_ALL, '')print("{:,d}".... -
Python 正则表达式验证有千位分隔符的数字
2014-09-12 10:14:391. Mandatory integer and fraction ^[0-9]{1,3}(,[0-9]{3})*\.[0-9]+$ 2. Mandatory integer and optional fraction. Decimal dot must be omitted if the fraction is omitted ^[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+) -
用逗号格式化数字以在Python中分隔成千上万
2021-07-16 12:54:16I have a large dataframe which has a column called Lead Rev. This column is a field of numbers such as (100000 or 5000 etc.) I want to know how to format these numbers to show commas as thousand separ... -
Python 正则表达式添加数字千位分隔符
2014-09-15 14:37:201. Basic soluation Match: [0-9](?=(?:[0-9]{3})+(?![0-9])) Replace: \g, 2. Match separator positions only, using lookbehind Match:(?[0-9])(?=(?:[0-9]{3})+(?![0-9])) Replace: ,