python语言环境货币转换为0小数

Han*_*ong 1 python format locale decimal

我不知道如何将我的货币设置为0个小数。目前,它总是使我的货币落后0.00。

locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
damn = locale.currency(self.damn, grouping=True).replace('$','') + " Dmn"
Run Code Online (Sandbox Code Playgroud)

self.damn始终是整数。

小智 6

看来您只是对分组感兴趣。您不需要为此使用货币功能。用途locale.format()

import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
damn = '{0} Dmn'.format(locale.format('%d', self.damn, True))
Run Code Online (Sandbox Code Playgroud)

如果您不依赖这些locale内容,也可以将数字与分组string.format()

# Comma as separator
damn = '{:,} Dmn'.format(self.damn)
# Locale aware separator
damn = '{:n} Dmn'.format(self.damn)
Run Code Online (Sandbox Code Playgroud)