gpt4 book ai didi

python - 使用 Python3 字符串格式化迷你语言打印带标点的大整数

转载 作者:太空狗 更新时间:2023-10-30 01:00:37 24 4
gpt4 key购买 nike

我想在大数的每三位数字后加一个点(例如 4.100.200.300)。

>>> x = 4100200300
>>> print('{}'.format(x))
4100200300

这个问题特定于 Python 的字符串格式化迷你语言。

最佳答案

只有一个可用的千位分隔符。

The ',' option signals the use of a comma for a thousands separator.

( docs )

例子:

'{:,}'.format(x) # 4,100,200,300

如果您需要使用点作为千位分隔符,请考虑将逗号替换为 '.' 或适当设置语言环境(LC_NUMERIC 类别)。

你可以使用 this列表以找到正确的语言环境。请注意,您必须使用 n 整数表示类型来进行语言环境感知格式化:

import locale
locale.setlocale(locale.LC_NUMERIC, 'de_DE') # or da_DK, or lt_LT, or mn_MN, or ...
'{:n}'.format(x) # 4.100.200.300

在我看来,前一种方法要简单得多:

'{:,}'.format(x).replace(',', '.') # 4.100.200.300

format(x, ',').replace(',', '.') # 4.100.200.300

关于python - 使用 Python3 字符串格式化迷你语言打印带标点的大整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33982160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com