I'm using python 3.8 in a Docker container. This is my docker file.
# Python image
FROM python:3.8.2-buster
# Install locales
RUN apt-get update
RUN apt-get install -y locales
RUN sed -i -e 's/# it_IT.UTF-8 UTF-8/it_IT.UTF-8 UTF-8/' /etc/locale.gen && locale-gen
#etc...
I wanna format numbers according to the Italian format (ie, .
as a thousands separator). Fortunately, the format-specification-mini-language says:
The '
,
' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n
' integer presentation type instead.
I run this code inside such a container:
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "it_IT.UTF-8")
>>> a = 10000000
>>> f'a is equal to {a:,}'
'a is equal to 10,000,000' # ok!
>>> f'a is equal to {a:n}'
'a is equal to 10000000' # What!? I expected 'a is equal to 10.000.000' instead
but the last line behaves unexpectedly.
Where am I wrong? Is that a problem with my locale definition or with my usage of numbers formatting harnessing n
?
Try this:
import locale
locale.setlocale(locale.LC_ALL, '')
a = 1000000
print(f'{a:n}') # => 1,000,000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.