f字符串中的整数表示类型未返回预期格式

问题描述 投票:3回答:1

我正在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说:

'[,'选项表示使用千位分隔符的逗号。对于支持区域设置的分隔符,请使用“ 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的数字格式使用是否有问题?

python docker locale string-formatting f-string
1个回答
0
投票

尝试一下:

import locale
locale.setlocale(locale.LC_ALL, '') 
a = 1000000    
print(f'{a:n}')  # => 1,000,000
© www.soinside.com 2019 - 2024. All rights reserved.