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.
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
:
>>> 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