我在 Docker 容器中使用 python 3.8。这是我的 docker 文件。
# 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...
我想根据意大利语格式设置数字格式(即,.
作为千位分隔符)。幸运的是,format-specification-mini-language说:
The '
,
' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n
' integer presentation type instead.
我在这样的容器中运行这段代码:
>>> 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
但最后一行的行为出乎意料。
我哪里错了?这是我的语言环境定义的问题还是我使用 n
的数字格式设置的问题?
最佳答案
试试这个:
import locale
locale.setlocale(locale.LC_ALL, '')
a = 1000000
print(f'{a:n}') # => 1,000,000
关于python - f 字符串中的整数表示类型不返回预期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60684451/