[英]Python: Thousands Separator - old Formatting Syntax
With the new formatting syntax, Python can easily be made to print a thousands separator: 使用新的格式化语法,可以轻松地使用Python打印数千个分隔符:
print ("{0:,.2f} Euro".format(myVariable))
So the comma takes care for this. 所以逗号会照顾这个。
How can this be done with the old syntax - without using locale? 如何使用旧语法完成此操作 - 不使用语言环境?
print ("%.2f Euro" % myVariable)
printf
-style formatting does not support thousand separator. printf
style格式不支持千位分隔符。
You need to build a string with thousand separator: 你需要用千位分隔符构建一个字符串:
>>> amount = 12345
>>> '%s Euro' % format(amount, ',.2f')
'12,345.00 Euro'
Built-in function format
uses the same Format Specification Mini-Language that str.format
uses. 内置函数format
使用与str.format
相同的格式规范迷你语言 。
If you can't use format
, see this question for alternatives. 如果您不能使用format
,请参阅此问题以获取替代方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.