python - 在 OSX 上使用格式语言的德语数字分隔符?

更新:到目前为止,答案显示这似乎是 OSX 上与平台相关的错误,与特定的区域设置有关,因为它们不完全支持数字分组。

更新2:我刚刚打开an issue在 Python 的错误跟踪器上。看看有没有办法解决这个问题。


我想根据德国编号约定格式化整数和 float 。这可以使用格式语言和表示类型 n 但在我的平台上失败。

  • 平台: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)] 在 darwin

例子:

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

到目前为止我所尝试的:

  1. 设置德语语言环境

    import locale
    locale.setlocale(locale.LC_ALL, 'de_DE')
    
  2. 格式规范选项,只识别英文格式。

    '{:,}'.format(1234)
    '1,234'
    
    '{:,}'.format(1234.56)
    '1,234.56'
    
    '{:,}'.format(1000000)
    '1,000,000'
    
  3. 根据Python docs ,整数和浮点表示类型 n 应该做我想做的,但它没有。

     '{: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
    
  4. 受@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)
    

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

        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
        ------------------------------------------------------------
    

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

        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
        ------------------------------------------------------------
    

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

最佳答案

super 丑陋,但从技术上回答了这个问题:

来自 PEP 378 :

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

https://stackoverflow.com/questions/14287051/

相关文章:

url - 查询字符串中是否有任何格式约定?

c# - 使用对象初始化器时修复缩进

r - 通过带有条件格式的 xlsx 将数据框导出到 Excel

html - 格式丰富的内容用什么标记语言?

c# - 大写的UTF-8?

c++ - 对于 GCC <5,是否有 std::put_time 的内置替代方案?

xcode4 - XCode 4 注释格式(换行)

layout - Mathematica 中的 2 列文档

r - xtable 用于条件单元格格式化表格的重要 p 值

python - 如何在 Python 格式说明符中使用变量名