Eli*_*ria 669 python number-formatting
我试图在Python 2.6.1中使用逗号作为千位分隔符打印一个整数.例如,我想将数字显示1234567为1,234,567.我该怎么做呢?我在Google上看过很多例子,但我正在寻找最简单实用的方法.
它不需要特定于语言环境来决定句点和逗号.我希望尽可能简单的事情.
小智 1614
对于Python≥2.7:
'{:,}'.format(value) # For Python ?2.7
f'{value:,}' # For Python ?3.7
Run Code Online (Sandbox Code Playgroud)
每格式规范迷你语言,
该
','选项表示使用逗号表示千位分隔符.对于区域设置感知分隔符,请改用'n'整数表示类型.
Mik*_*one 282
我得到了这个工作:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US')
'en_US'
>>> locale.format("%d", 1255000, grouping=True)
'1,255,000'
Run Code Online (Sandbox Code Playgroud)
当然,您不需要国际化支持,但它清晰,简洁,并使用内置库.
PS"%d"是通常的%式格式化程序.您可以只有一个格式化程序,但它可以是字段宽度和精度设置方面的任何需要.
PPS如果你不能locale上班,我建议马克答案的修改版本:
def intWithCommas(x):
if type(x) not in [type(0), type(0L)]:
raise TypeError("Parameter must be an integer.")
if x < 0:
return '-' + intWithCommas(-x)
result = ''
while x >= 1000:
x, r = divmod(x, 1000)
result = ",%03d%s" % (r, result)
return "%d%s" % (x, result)
Run Code Online (Sandbox Code Playgroud)
递归对于负面情况很有用,但每个逗号的一次递归对我来说似乎有些过分.
Kas*_*ham 118
由于效率低下和不可读性,很难被击败:
>>> import itertools
>>> s = '-1234567'
>>> ','.join(["%s%s%s" % (x[0], x[1] or '', x[2] or '') for x in itertools.izip_longest(s[::-1][::3], s[::-1][1::3], s[::-1][2::3])])[::-1].replace('-,','-')
Run Code Online (Sandbox Code Playgroud)
Nad*_*mli 93
以下是删除不相关部分并稍微清理后的区域设置分组代码:
(以下仅适用于整数)
def group(number):
s = '%d' % number
groups = []
while s and s[-1].isdigit():
groups.append(s[-3:])
s = s[:-3]
return s + ','.join(reversed(groups))
>>> group(-23432432434.34)
'-23,432,432,434'
Run Code Online (Sandbox Code Playgroud)
这里已经有了一些很好的答案.我只想添加它以供将来参考.在python 2.7中,将有一个千位分隔符的格式说明符.根据python docs,它的工作原理如下
>>> '{:20,.2f}'.format(f)
'18,446,744,073,709,551,616.00'
Run Code Online (Sandbox Code Playgroud)
在python3.1中你可以这样做:
>>> format(1234567, ',d')
'1,234,567'
Run Code Online (Sandbox Code Playgroud)
Emi*_*röm 72
我很惊讶没有人提到你可以用Python 3.6中的f字符串做到这一点就像这样简单:
>>> num = 10000000
>>> print(f"{num:,d}")
10,000,000
Run Code Online (Sandbox Code Playgroud)
...冒号后面的部分是格式说明符.逗号是您想要的分隔符,因此f"{num:_d}"使用下划线而不是逗号.
这相当于使用format(num, ",d")旧版本的python 3.
Dan*_*nov 37
这是一个单行正则表达式替换:
re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%d" % val)
Run Code Online (Sandbox Code Playgroud)
仅适用于非常规输出:
import re
val = 1234567890
re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%d" % val)
# Returns: '1,234,567,890'
val = 1234567890.1234567890
# Returns: '1,234,567,890'
Run Code Online (Sandbox Code Playgroud)
或者对于少于4位数的浮点数,请将格式说明符更改为%.3f:
re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%.3f" % val)
# Returns: '1,234,567,890.123'
Run Code Online (Sandbox Code Playgroud)
注意:如果超过三位小数,则无法正常工作,因为它会尝试对小数部分进行分组:
re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%.5f" % val)
# Returns: '1,234,567,890.12,346'
Run Code Online (Sandbox Code Playgroud)
让我们分解一下:
re.sub(pattern, repl, string)
pattern = \
"(\d) # Find one digit...
(?= # that is followed by...
(\d{3})+ # one or more groups of three digits...
(?!\d) # which are not followed by any more digits.
)",
repl = \
r"\1,", # Replace that one digit by itself, followed by a comma,
# and continue looking for more matches later in the string.
# (re.sub() replaces all matches it finds in the input)
string = \
"%d" % val # Format the string as a decimal to begin with
Run Code Online (Sandbox Code Playgroud)
elP*_*tor 25
这就是我为花车做的事情.虽然,老实说,我不确定它适用于哪个版本 - 我使用的是2.7:
my_number = 4385893.382939491
my_string = '{:0,.2f}'.format(my_number)
Run Code Online (Sandbox Code Playgroud)
返回:4,385,893.38
更新:我最近遇到了这种格式的问题(无法告诉您具体原因),但能够通过删除以下内容来修复它0:
my_string = '{:,.2f}'.format(my_number)
Run Code Online (Sandbox Code Playgroud)
小智 19
您还可以使用'{:n}'.format( value )区域设置表示.我认为这是区域设置解决方案最简单的方法.
有关更多信息,请thousands在Python DOC中搜索.
对于货币,您可以使用locale.currency,设置标志grouping:
码
import locale
locale.setlocale( locale.LC_ALL, '' )
locale.currency( 1234567.89, grouping = True )
Run Code Online (Sandbox Code Playgroud)
产量
'Portuguese_Brazil.1252'
'R$ 1.234.567,89'
Run Code Online (Sandbox Code Playgroud)
lee*_*emm 19
最简单的答案:
format(123456, ",")
Run Code Online (Sandbox Code Playgroud)
结果:
'123,456'
Run Code Online (Sandbox Code Playgroud)
小智 16
以下是一些格式化方法(与浮点数和整数兼容)
num = 2437.68
# Way 1: String Formatting
'{:,}'.format(num)
>>> '2,437.68'
# Way 2: F-Strings
f'{num:,}'
>>> '2,437.68'
# Way 3: Built-in Format Function
format(num, ',')
>>> '2,437.68'
Run Code Online (Sandbox Code Playgroud)
Mar*_*ers 11
我确信必须有一个标准的库函数,但尝试使用递归自己编写它很有趣所以这就是我想出的:
def intToStringWithCommas(x):
if type(x) is not int and type(x) is not long:
raise TypeError("Not an integer!")
if x < 0:
return '-' + intToStringWithCommas(-x)
elif x < 1000:
return str(x)
else:
return intToStringWithCommas(x / 1000) + ',' + '%03d' % (x % 1000)
Run Code Online (Sandbox Code Playgroud)
话虽如此,如果其他人确实找到了标准的方法,你应该使用它.
Mar*_*oma 10
略微扩大Ian Schneider的答案:
如果要使用自定义千位分隔符,最简单的解决方案是:
'{:,}'.format(value).replace(',', your_custom_thousands_separator)
Run Code Online (Sandbox Code Playgroud)
'{:,.2f}'.format(123456789.012345).replace(',', ' ')
Run Code Online (Sandbox Code Playgroud)
如果你想要这样的德语表示,它会变得有点复杂:
('{:,.2f}'.format(123456789.012345)
.replace(',', ' ') # 'save' the thousands separators
.replace('.', ',') # dot to comma
.replace(' ', '.')) # thousand separators to dot
Run Code Online (Sandbox Code Playgroud)
从评论到activestate recipe 498181我重写了这个:
import re
def thous(x, sep=',', dot='.'):
num, _, frac = str(x).partition(dot)
num = re.sub(r'(\d{3})(?=\d)', r'\1'+sep, num[::-1])[::-1]
if frac:
num += dot + frac
return num
Run Code Online (Sandbox Code Playgroud)
它使用正则表达式功能:lookahead即(?=\d)确保只有三个数字的组有一个'after'后面的数字才能得到一个逗号.我说'之后'因为此时字符串是反向的.
[::-1] 只是反转一个字符串.
接受的答案很好,但实际上我更喜欢format(number,',').我更容易理解和记忆.
https://docs.python.org/3/library/functions.html#format
-
整数(不带小数):
"{:,d}".format(1234567)
-
浮点数(带小数):
"{:,.2f}".format(1234567)
其中number之前f指定小数位数.
-
奖金
印度lakhs/crores编号系统(12,34,567)的快速启动功能:
小智 6
从 Python 2.6 版开始,您可以执行以下操作:
def format_builtin(n):
return format(n, ',')
Run Code Online (Sandbox Code Playgroud)
对于 Python 版本 < 2.6,仅供参考,这里有 2 个手动解决方案,它们将浮点数转换为整数,但负数可以正常工作:
def format_number_using_lists(number):
string = '%d' % number
result_list = list(string)
indexes = range(len(string))
for index in indexes[::-3][1:]:
if result_list[index] != '-':
result_list.insert(index+1, ',')
return ''.join(result_list)
Run Code Online (Sandbox Code Playgroud)
这里有几点需要注意:
还有一个更硬核的版本:
def format_number_using_generators_and_list_comprehensions(number):
string = '%d' % number
generator = reversed(
[
value+',' if (index!=0 and value!='-' and index%3==0) else value
for index,value in enumerate(reversed(string))
]
)
return ''.join(generator)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
393388 次 |
| 最近记录: |