Skip to content Skip to sidebar Skip to footer

How To Convert Numbers To String With Decimal And Commas

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

Solution 1:

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'

Solution 2:

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.

Post a Comment for "How To Convert Numbers To String With Decimal And Commas"