将长数字格式化为字符串
在Python中,有什么简单的方法可以将整数格式化为字符串,用K表示千位,用M表示百万位,并且在逗号后只留下几个数字?
我想将 7436313 显示为 7.44M,将 2345 显示为 2,34K。
是否有一些 % 字符串格式化运算符可用? 或者只能通过在循环中实际除以 1000 并逐步构造结果字符串来完成?
What is an easy way in Python to format integers into strings representing thousands with K, and millions with M, and leaving just couple digits after comma?
I'd like to show 7436313 as 7.44M, and 2345 as 2,34K.
Is there some % string formatting operator available for that? Or that could be done only by actually dividing by 1000 in a loop and constructing result string step by step?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
这个版本不会受到之前答案中 999,999 为您提供 1000.0K 的错误的影响。 它还只允许 3 个有效数字并消除尾随 0。
输出看起来像:
This version does not suffer from the bug in the previous answers where 999,999 gives you 1000.0K. It also only allows 3 significant figures and eliminates trailing 0's.
The output looks like:
我认为没有内置函数可以做到这一点。 你必须自己动手,例如:
I don't think there's a built-in function that does that. You'll have to roll your own, e.g.:
更“数学”的解决方案是使用 math.log:
测试:
A more "math-y" solution is to use
math.log
:Tests:
精度可变且无 999999 bug:
Variable precision and no 999999 bug:
我今天需要这个函数,为 Python >= 3.6 的人刷新了接受的答案:
编辑:鉴于注释,您可能需要更改为
round(num/1000.0)
I needed this function today, refreshed the accepted answer a bit for people with Python >= 3.6:
Edit: given the comments, you might want to change to
round(num/1000.0)
Numerize 库很好。
感谢@tdy 指出这一点,
Numerize library is good.
Thanks @tdy for pointing this,
我对其他人展示的一些内容感到有点困惑,所以我编写了下面的代码。 它四舍五入到小数点后第二位,例如。 “235.6 亿”,但您可以通过用更大或更小的数字替换最后一行中的两个“100.0”来更改其舍入到的小数位,例如。 “10.0”四舍五入到小数点后一位,“1000.0”四舍五入到小数点后三位。 此外,使用此代码时,它总是从实际值向下舍入。 如果您愿意,可以更改此设置,将“地板”替换为“天花板”或“圆形”。
最后一行字符串之前的“f”是为了让 python 知道你正在格式化它。 运行 print(simplify(34867123012.13)) 的结果是这样的:
如果您有疑问,请告诉我!
谢谢,
安格斯
I was kind of confused by some of the stuff that other people showed, so I made the below code. It rounds to the second decimal point, ex. '23.56 Billion', but you can change what decimal place it rounds to by replacing the two '100.0's in the last line with a larger or smaller number, ex. '10.0' rounds to one decimal point and '1000.0' rounds to three decimal points. Also, using this code, it always rounds down from what it actually is. You can change this if you like, by replacing 'floor' with 'ceil' or 'round'.
The 'f' before the string in the last line is to let python know you are formatting it. The result from running print(simplify(34867123012.13)) is this:
Please let me know if you have questions!
Thanks,
Angus
我有同样的需要。 如果有人涉及这个主题,我找到了一个库来这样做: https://github.com/azaitsev/ millify
希望有帮助:)
I had the same need. And if anyone comes on this topic, I found a lib to do so: https://github.com/azaitsev/millify
Hope it helps :)
根据这里的评论,我为此编写了改进的代码。 它有点长,但给出了更多情况的解决方案,包括小数字(m,u,n,p)。
有帮助
希望这对从这里进行测试的人和其他人
Based on the comments here, I made an improved code for that. It is a little bit longer but gives solutions for more cases including small numbers (m,u,n,p).
Hope it will be helpful for someone
tests from here and some more
我知道这是一个老问题,但我想补充一句:
I know this is an old question but I'd like to add this one-liner:
没有字符串格式化运算符,根据文档。 我从来没有听说过这样的事情,所以你可能必须按照你的建议自己动手。
No String Formatting Operator, according to the docs. I've never heard of such a thing, so you may have to roll your own, as you suggest.
我认为没有格式运算符,但您可以简单地除以 1000,直到结果介于 1 和 999 之间,然后使用逗号后的 2 位数字的格式字符串。 在大多数情况下,单位是单个字符(或者可能是一个小字符串),您可以将其存储在字符串或数组中,并在每次除法后对其进行迭代。
I don't think there are format operators for that, but you can simply divide by 1000 until the result is between 1 and 999 and then use a format string for 2 digits after comma. Unit is a single character (or perhaps a small string) in most cases, which you can store in a string or array and iterate through it after each divide.
我不知道有任何这样的内置功能,但这里有几个可能有帮助的列表线程:
http://coding.derkeiler.com/Archive/Python/comp.lang.python/2005-09/msg03327.html
http://mail.python.org/pipermail/python-列表/2008-8月/503417.html
I don't know of any built-in capability like this, but here are a couple of list threads that may help:
http://coding.derkeiler.com/Archive/Python/comp.lang.python/2005-09/msg03327.html
http://mail.python.org/pipermail/python-list/2008-August/503417.html
刚刚为此创建了一个函数:
基本:
高级:
Just made a function for this:
Basic:
Advanced: