在OSX上使用格式语言的德语数字分隔符? [英] German number separators using format language on OSX?

查看:178
本文介绍了在OSX上使用格式语言的德语数字分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据德语编号约定格式化整数和浮点数.使用格式语言和演示文稿类型n,这是可能的,但在我的平台上失败.

I want to format integer and float numbers according to the German numbering convention. This is possible using the format language and the presentation type n but fails on my platform.

  • 平台:OS X 10.8.2(Mountain Lion)
  • Python:2.7.3 64位(v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

示例:

  • 1234 => 1.234
  • 1234.56 => 1.234,56
  • 1000000 => 1.000.000
  • 1234 => 1.234
  • 1234.56 => 1.234,56
  • 1000000 => 1.000.000

到目前为止我尝试过的事情:

What I have tried so far:

  1. 设置德语语言环境

  1. Setting the German locale

import locale
locale.setlocale(locale.LC_ALL, 'de_DE')

  • 格式说明选项,仅识别英语格式.

  • The format specification option , only recognizes the English format.

    '{:,}'.format(1234)
    '1,234'
    
    '{:,}'.format(1234.56)
    '1,234.56'
    
    '{:,}'.format(1000000)
    '1,000,000'
    

  • 根据 Python文档,整数和浮点表示类型n应该可以执行我想要的操作,但是不可以.

  • According to the Python docs, the integer and float presentation type n is supposed to do what I want but it doesn't.

     '{:n}'.format(1234)
     '1234'
    
     '{:n}'.format(1234.56)
     '1234,56'  # at least the comma was set correctly here
    
     '{:n}'.format(1000000)
     '1000000'
    
     '{:n}'.format(12345769.56)
     '1,23458e+07'  # it's doing weird things for large floats
    

  • 受@ J.F.Sebastian启发的更多示例和比较:

  • Some more examples and comparisons inspired by @J.F.Sebastian:

    for n in [1234, 1234.56, 1000000, 12345769.56]:
        print('{0:,} {0:n}'.format(n))
        fmt, val = "%d %f", (n, n)
        print(fmt % val)
        print(locale.format_string(fmt, val))
        print(locale.format_string(fmt, val, grouping=True))
        print('-'*60)
    

    这会在我的平台上产生以下错误结果:

    This yields the following incorrect results on my platform:

        1,234 1234
        1234 1234.000000
        1234 1234,000000
        1234 1234,000000
        ------------------------------------------------------------
        1,234.56 1234,56
        1234 1234.560000
        1234 1234,560000
        1234 1234,560000
        ------------------------------------------------------------
        1,000,000 1000000
        1000000 1000000.000000
        1000000 1000000,000000
        1000000 1000000,000000
        ------------------------------------------------------------
        12,345,769.56 1,23458e+07
        12345769 12345769.560000
        12345769 12345769,560000
        12345769 12345769,560000
        ------------------------------------------------------------
    

    我没有得到的正确结果如下:

    The correct results which I'm not getting would look like that:

        1,234 1.234
        1234 1234.000000
        1234 1234,000000
        1.234 1.234,000000
        ------------------------------------------------------------
        1,234.56 1.234,56
        1234 1234.560000
        1234 1234,560000
        1.234 1.234,560000
        ------------------------------------------------------------
        1,000,000 1.000.000
        1000000 1000000.000000
        1000000 1000000,000000
        1.000.000 1.000.000,000000
        ------------------------------------------------------------
        12,345,769.56 1,23458e+07 
        12345769 12345769.560000
        12345769 12345769,560000
        12.345.769 12.345.769,560000
        ------------------------------------------------------------
    

  • 您仅使用格式语言对我有解决方案吗?有什么办法欺骗我平台上的语言环境设置以接受分组?

    Do you have a solution for me using the format language only? Is there any way to trick the locale settings on my platform to accept grouping?

    推荐答案

    超级丑陋,但从技术上讲可以回答以下问题:

    Super ugly, but technically answers the question:

    来自 PEP 378 :

    '{:,}'.format(1234.56).replace(",", "X").replace(".", ",").replace("X", ".")
    '1.234,56'
    

    这篇关于在OSX上使用格式语言的德语数字分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆