简体   繁体   English

格式化数字,空格为千位分隔符,逗号为小数

[英]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.

相关问题 如何用逗号将轴号格式格式化为千位 - How to format axis number format to thousands with a comma 使用点或逗号作为分隔符的带或不带小数的数字的 Python 正则表达式? - Python regex for number with or without decimals using a dot or comma as separator? 将千位逗号分隔符添加到 Relplot - Add thousands comma separator to Relplot 如果 number>=10,000,则用逗号将轴号格式化为千位 - Format axis number to thousands with a comma if number>=10,000 Pandas to_csv 如何使用逗号作为千位分隔符格式化 int 和 float 变量 - Pandas to_csv how to format both int and float variables with comma as thousands separator 在注释值中添加千位逗号分隔符 - Python - Adding thousands comma separator to annotated values - Python 如何在数据框中的字符串中查找数字并使用千位分隔符重新格式化该数字? - How to find number within a string in dataframe and re-format that number using thousands separator? matplotlib 中以空格作为千位分隔符的科学刻度数 - Scientific tick numbers with space as thousands separator in matplotlib 如何将格式字符串中的千位分隔符更改为撇号 - How to change thousands separator to apostrophe in format string python .format指定字段宽度中的千位分隔符 - python .format thousands separator in specified field width
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM