如何在当前语言环境中格式化Python中的浮点数

时间:2018-03-17 00:42:08

标签: python-3.x internationalization

我想根据当前的语言环境在我的Python应用程序中格式化浮点数。 Format Specification Mini-Language表示“浮点和小数值的可用表示类型”之一是:

  

'n'号码。这与'g'相同,只是它使用当前的语言环境设置插入适当的数字分隔符。

但我不能让它发挥作用。这就是我尝试的方式:

class SVR(pb.Root):
    def remote_get_msg(self):
        return 'hello'

    def connectionLost(self, reason):
        print reason

    def connectionMade(self):
        print "Connection success! Connected to:", self.transport.getPeer().host, ':', self.transport.getPeer().port

class Twin_ServerApp(App):
    label = None

    def build(self):
        self.label = Label(text="server started\n")
        reactor.listenTCP(SERVER_PORT, pb.PBServerFactory(SVR()))
        return self.label

我也尝试过:

$ unset LC_ALL
$ unset LANG
$ export LANG=de_DE.UTF-8
$ python3 -c "import locale; print (locale.getdefaultlocale())"
('de_DE', 'UTF-8')

$ python3 -c "print ('{0:.3n}'.format(3.14))"
3.14

我希望所有这些都打印$ python3 -c "import locale; print (locale.str(3.14))" 3.14 $ python3 -c "import locale; print (locale.format_string('%.2f', 3.14))" 3.14 ,而不是3,14。知道什么是错的吗?

0 个答案:

没有答案