-4

I'd like to convert a number like 1323.67 to 1.323,67, how can I do that?

I've tried this

f'{1325.76:.,2f}'

but it prints out 1,325.76

I excpected f'{1325.76:.,2f}' to be 1.325,75 but it's 1,325.76

  • 1
    requirement does not seems to appropriate – Roushan Aug 10 '19 at 07:13
  • Possible duplicate of [How to print number with commas as thousands separators?](https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators) – Håken Lid Aug 10 '19 at 15:43

2 Answers2

5

If you can use external modules, I would suggest you to use babel

>>> from babel.numbers import format_decimal
>>> format_decimal(1323.67, locale='de_DE')
'1.323,67'
Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • Instead of introducing another dependence, we can just correct the f-formatting – Prayson W. Daniel Aug 10 '19 at 07:29
  • I like this despite the dependency because it makes the intent clear. I couldn't understand why the OP has such a strange formatting requirement but it turns out, it's a thing of a specific locale. – ivan_pozdeev Aug 10 '19 at 15:41
1

The format is

f'{1325.76:,.2f}' and not f'{1325.76:.,2f}'

:,.2f is what you want. Which means , as separator with 2 decimal positions.

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57