e324. formatting a message containing a number.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 24 行

TXT
24
字号
Object[] params = new Object[]{new Integer(123), new Integer(1234)};
    String msg = MessageFormat.format("There are {0} a''s and {1} b''s", params);
    // There are 123 a's and 1,234 b's
    
    msg = MessageFormat.format("There are {0,number} a''s and {1,number} b''s", params);
    // There are 123 a's and 1,234 b's
    
    // Use a custom format; see e311 Formatting a Number Using a Custom Format
    msg = MessageFormat.format("There are {0,number,#} a''s and {1,number,#} b''s", params);
    // There are 123 a's and 1234 b's
    
    // Floating point numbers
    params = new Object[]{new Double(123.45), new Double(1234.56)};
    msg = MessageFormat.format("There are {0,number,#.#} a''s and {1,number,#.#} b''s", params);
    // There are 123.4 a's and 1234.6 b's
    
    // Currency
    msg = MessageFormat.format("There are {0,number,currency} a''s and {1,number,currency} b''s", params);
    // There are $123.00 a's and $1,234.00 b's
    
    // Percent
    msg = MessageFormat.format("There are {0,number,percent} a''s and {1,number,percent} b''s", params);
    // There are 12,345% a's and 123,456% b's

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?