[英]Format number with space as thousands separator and comma for decimals
I would like to format these numbers to the corresponding format: 我想将这些数字格式化为相应的格式:
3911.940 -> 3 911,94
1970 -> 1 970,00
393.75000 -> 393,75
300000.50 -> 300 000,50
... but I can't figure out how to do this elegantly. ...但是我不知道如何优雅地做到这一点。
Thanks for any help solving this. 感谢您为解决此问题提供的帮助。
You can use django.utils.numberformat.format
: 您可以使用django.utils.numberformat.format
:
>>> from django.utils import numberformat
>>> for i in [3911.940, 1970, 393.75000, 300000.50]:
... print(i, ' -> ', numberformat.format(i, decimal_sep=',', decimal_pos=2, grouping=3, thousand_sep=' ', force_grouping=True))
...
3911.94 -> 3 911,94
1970 -> 1 970,00
393.75 -> 393,75
300000.5 -> 300 000,50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.