有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用NumberFormat格式化数字时的java字段位置

有人能用通俗易懂的英语解释下面从这个API中摘取的内容吗

You can also use forms of the parse and format methods with ParsePosition and FieldPosition to allow you to:

progressively parse through pieces of a string

align the decimal point and other areas

For example, you can align numbers in two ways: If you are using a monospaced font with spacing for alignment, you can pass the FieldPosition in your format call, with field = INTEGER_FIELD. On output, getEndIndex will be set to the offset between the last character of the integer and the decimal. Add (desiredSpaceCount - getEndIndex) spaces at the front of the string.

我不知道FieldPosition有什么用,上面发布的API也没有帮助(至少对我是这样)。 一个简单的例子显示了输出将是超级! 提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    NumberFormat is the abstract base class for all number formats. This class provides the interface for formatting and parsing numbers.

    要使用数字格式,首先必须获得一个区域设置实例

    然后您可以设置格式化的许多属性。例如,您可以选择显示逗号、限制小数位数以及设置最小和最大整数长度。如果要相对于区域设置显示“%”,则必须使用NumberFormat。只是不要将“%”作为字符串附加到结果中。你想用(3745)代替“-”来表示一个负数,然后用NumberFormat。像这些,有许多用途

    您可以检查JavaDoc以了解更多方法

    这会告诉你怎么做

        NumberFormat numberFormat = NumberFormat.getInstance();
    
        // setting number of decimal places
        numberFormat.setMinimumFractionDigits(2);
        numberFormat.setMaximumFractionDigits(2);
    
        // you can also define the length of integer
        // that is the count of digits before the decimal point
        numberFormat.setMinimumIntegerDigits(1);
        numberFormat.setMaximumIntegerDigits(10);
    
        // if you want the number format to have commas
        // to separate the decimals the set as true
        numberFormat.setGroupingUsed(true);
    
        // convert from integer to String
        String formattedNr = numberFormat.format(12345678L);
        // note that the output will have 00 in decimal place
    
    
        // convert from decimal to String
        numberFormat.format(12345.671D);
    
        // format a String to number
        Number n1 = null;
        Number n2 = null;
    
          n1 = numberFormat.parse("1,234");
          n2 = numberFormat.parse("1.234");
    
        // show percentage
        numberFormat = NumberFormat.getPercentInstance();
        numberFormat.format(0.98);
        // answer will be 98%
    

    这就是如何使用数字格式的字段位置

    // Get a default NumberFormat instance.
            NumberFormat numForm = NumberFormat.getInstance();
    
            // Format some decimals using the pattern supplied above.
            StringBuffer dest1 = new StringBuffer(24);
            StringBuffer dest2 = new StringBuffer(24);
            FieldPosition pos = new FieldPosition(NumberFormat.FRACTION_FIELD);
    
            dest1 = numForm.format(22.3423D, dest1, pos);
            System.out.println("dest1 = " + dest1);
            System.out.println("FRACTION is at: " + pos.getBeginIndex() +
                ", " + pos.getEndIndex());
    
            dest2 = numForm.format(64000D, dest2, pos);
            System.out.println("dest2 = " + dest2);
            System.out.println("FRACTION is at: " + pos.getBeginIndex() +
                ", " + pos.getEndIndex());
    /*
    Output:
    dest1 = 22.342
    FRACTION is at: 3, 6
    dest2 = 64,000
    FRACTION is at: 6, 6
    */
    
  2. # 2 楼答案

    FieldPositionParsePosition类的java文档给出了更多提示

    基本上,如果您不想格式化一个完整的日期或数字,而只想格式化其中的一部分(例如,如果您的UI将金额拆分为几个部分(例如在两个输出字段中提供美元和美分),则可以使用FieldPosition。如果您需要类似的东西,您可以使用FieldPosition来检索您感兴趣的零件。 对于ParsePosition,我现在没有一个好的用例,也许其他人可以在这里提供帮助