【发布时间】:2010-10-09 10:08:09
【问题描述】:
在 Python 中,有什么简单的方法可以将整数格式化为字符串,用 K 表示千位,用 M 表示百万位,并且在逗号后面只留下几个数字?
我想将 7436313 显示为 7.44M,将 2345 显示为 2,34K。
是否有一些可用的 % 字符串格式化运算符?或者只能在循环中实际除以 1000 并逐步构造结果字符串来完成?
【问题讨论】:
标签: python formatting string integer
在 Python 中,有什么简单的方法可以将整数格式化为字符串,用 K 表示千位,用 M 表示百万位,并且在逗号后面只留下几个数字?
我想将 7436313 显示为 7.44M,将 2345 显示为 2,34K。
是否有一些可用的 % 字符串格式化运算符?或者只能在循环中实际除以 1000 并逐步构造结果字符串来完成?
【问题讨论】:
标签: python formatting string integer
Numerize库不错。
from numerize import numerize
a = numerize.numerize(1000)
print(a)
1k
感谢@tdy 指出这一点,
a = numerize.numerize(999999)
print(a) # 1000K
1000K
【讨论】:
可变精度且无 999999 错误:
def human_format(num, round_to=2):
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num = round(num / 1000.0, round_to)
return '{:.{}f}{}'.format(num, round_to, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])
【讨论】:
round(num, round_to) 替换为num,因为它已经被四舍五入了。
我对其他人展示的一些东西有点困惑,所以我编写了以下代码。它四舍五入到第二个小数点,例如。 '235.6 亿',但您可以通过将最后一行中的两个 '100.0' 替换为更大或更小的数字来更改其四舍五入的小数位,例如。 “10.0”四舍五入到小数点后一位,“1000.0”四舍五入到小数点后三位。此外,使用此代码,它总是从实际值向下取整。您可以根据需要更改此设置,将 'floor' 替换为 'ceil' 或 'round'。
#make the dictionary to store what to put after the result (ex. 'Billion'). You can go further with this then I did, or to wherever you wish.
#import the desired rounding mechanism. You will not need to do this for round.
from math import floor
magnitudeDict={0:'', 1:'Thousand', 2:'Million', 3:'Billion', 4:'Trillion', 5:'Quadrillion', 6:'Quintillion', 7:'Sextillion', 8:'Septillion', 9:'Octillion', 10:'Nonillion', 11:'Decillion'}
def simplify(num):
num=floor(num)
magnitude=0
while num>=1000.0:
magnitude+=1
num=num/1000.0
return(f'{floor(num*100.0)/100.0} {magnitudeDict[magnitude]}')
最后一行字符串前面的'f'是为了让python知道你正在格式化它。运行 print(simplify(34867123012.13)) 的结果是这样的:
34.86 Billion
如果您有任何问题,请告诉我! 谢谢, 安格斯
【讨论】:
1 Million 时,它会显示 999.99 Thousand。这也只能在 Python 3.6+ 上使用
我也有同样的需要。如果有人谈到这个话题,我找到了一个库来这样做:https://github.com/azaitsev/millify
希望对你有帮助:)
【讨论】:
此版本不受先前答案中的错误的影响,即 999,999 给您 1000.0K。它还只允许 3 个有效数字并消除尾随的 0。
def human_format(num):
num = float('{:.3g}'.format(num))
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
return '{}{}'.format('{:f}'.format(num).rstrip('0').rstrip('.'), ['', 'K', 'M', 'B', 'T'][magnitude])
输出如下:
>>> human_format(999999)
'1M'
>>> human_format(999499)
'999K'
>>> human_format(9994)
'9.99K'
>>> human_format(9900)
'9.9K'
>>> human_format(6543165413)
'6.54B'
【讨论】:
'9_9K' 而不是'9.9K'?
我今天需要这个函数,为 Python >= 3.6 的人刷新了接受的答案:
def human_format(num, precision=2, suffixes=['', 'K', 'M', 'G', 'T', 'P']):
m = sum([abs(num/1000.0**x) >= 1 for x in range(1, len(suffixes))])
return f'{num/1000.0**m:.{precision}f}{suffixes[m]}'
print('the answer is %s' % human_format(7454538)) # prints 'the answer is 7.45M'
编辑:给定 cmets,您可能希望更改为 round(num/1000.0)
【讨论】:
m = int(math.log10(num) // 3)
更“数学”的解决方案是使用math.log:
from math import log, floor
def human_format(number):
units = ['', 'K', 'M', 'G', 'T', 'P']
k = 1000.0
magnitude = int(floor(log(number, k)))
return '%.2f%s' % (number / k**magnitude, units[magnitude])
测试:
>>> human_format(123456)
'123.46K'
>>> human_format(123456789)
'123.46M'
>>> human_format(1234567890)
'1.23G'
【讨论】:
magnitude = int(floor(log(abs(number), k)))
magnitude = int(math.floor(math.log(max(abs(number), 1), k)))
我认为没有内置函数可以做到这一点。您必须自己滚动,例如:
def human_format(num):
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
# add more suffixes if you need them
return '%.2f%s' % (num, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])
print('the answer is %s' % human_format(7436313)) # prints 'the answer is 7.44M'
【讨论】:
long_nr=1_000_000而不是1000000,python不会考虑_..
【讨论】:
我认为没有格式运算符,但您可以简单地除以 1000,直到结果介于 1 和 999 之间,然后使用格式字符串表示逗号后的 2 位数字。在大多数情况下,单位是单个字符(或者可能是一个小字符串),您可以将其存储在字符串或数组中,并在每次划分后对其进行迭代。
【讨论】:
无字符串格式操作符,according to the docs。我从来没有听说过这样的事情,所以你可能不得不按照你的建议自己滚动。
【讨论】: