How do I print floats with specified number of decimals and locale specific separators in Python?

Viewed 60

I'm writing an application that needs to handle a fixed number of decimal places for floating-point numbers, which is easy enough with the f presentation type.

>>> f'{1.1:.2f}'
'1.10'

I also need number separators to be locale-aware as if using the n presentation type but n uses the same logic that g does to handle precision, so I end up losing the trailing zero (I need this)

>>> f'{1.1:.2n}'
'1,1'

Even worse, "n" shifts to e and loses digits for larger values where f keeps behaving sanely:

>>> f'{1234.:.2n}'
'1,2e+03'
>>> f'{1234.:.2f}'
'1234.00'

Is it possible to handle numbers the way f does, but with locale awareness of n without having to write my own formatting function?

0 Answers
Related