【问题标题】:FieldPosition in formatting a number with NumberFormat使用 NumberFormat 格式化数字的 FieldPosition
【发布时间】:2013-08-25 19:32:04
【问题描述】:

谁能用简单的英语解释来自API 的以下内容?

您还可以使用 parse 和 format 方法的形式 ParsePosition 和 FieldPosition 允许您:

逐步解析字符串的片段

对齐小数点和其他区域

例如,您可以通过两种方式对齐数字: 如果您使用带有间距的等宽字体进行对齐,您可以通过 格式调用中的 FieldPosition,字段 = INTEGER_FIELD。在输出时,getEndIndex 将被设置为偏移量 在整数的最后一个字符和小数之间。添加 (desiredSpaceCount - getEndIndex) 字符串前面的空格。

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

【问题讨论】:

    标签: java number-formatting decimalformat


    【解决方案1】:

    FieldPositionParsePosition 类的 java 文档提供了更多提示。

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

    【讨论】:

      【解决方案2】:

      NumberFormat 是所有数字格式的抽象基类。这个类提供了格式化和解析数字的接口。

      要使用数字格式,首先您必须获得一个语言环境实例

      然后可以设置很多格式的属性。例如,您可以选择显示逗号、限制小数位数以及设置最小和最大整数长度。如果你想显示关于语言环境的“%”,那么你必须使用 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
      */
      

      【讨论】:

      • 你没有回答我的问题。
      • @Rollerball 对不起,我迷路了 ;-) 希望你能理解:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      相关资源
      最近更新 更多