[英]Integer presentation type in f-strings does not return expected format
我在 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...
我想根据意大利格式(即.
作为千位分隔符)格式化数字。 幸运的是, 格式规范迷你语言说:
'
,
' 选项表示使用逗号作为千位分隔符。 对于区域设置识别分隔符,请改用“n
”整数表示类型。
我在这样的容器中运行此代码:
>>> 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.