模块 java.base
 java.text

类 ChoiceFormat

所有已实现的接口:
Serializable , Cloneable

public class ChoiceFormat extends NumberFormat
ChoiceFormat 允许您将格式附加到一系列数字。它通常在MessageFormat 中用于处理复数。选择是用双精度的升序列表指定的,其中每个项目指定到下一个项目的半开间隔:
 X matches j if and only if limit[j] ≤ X < limit[j+1]
 
如果没有匹配项,则使用第一个或最后一个索引,具体取决于数字 (X) 是太低还是太高。如果limit数组不是升序排列,格式化的结果会不正确。 ChoiceFormat 还接受 \u221E 等同于 infinity(INF)。

Note: ChoiceFormat 与其他 Format 类的不同之处在于,您使用构造函数(而不是 getInstance 样式的工厂方法)创建一个 ChoiceFormat 对象。工厂方法不是必需的,因为 ChoiceFormat 不需要为给定的locale进行任何复杂的设置。事实上,ChoiceFormat 没有实现任何特定于locale的行为。

创建 ChoiceFormat 时,必须指定格式数组和限制数组。这些数组的长度必须相同。例如,

  • limits = {1,2,3,4,5,6,7}
    formats = {"周日","周一","周二","周三","周四","周五","周六"}
  • limits = {0, 1, ChoiceFormat.nextDouble(1)}
    formats = {“无文件”、“一个文件”、“多个文件”}
    nextDouble 可用于获得下一个更高的 double,以形成半开区间。)

这是一个显示格式化和解析的简单示例:


 double[] limits = {1,2,3,4,5,6,7};
 String[] dayOfWeekNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
 ChoiceFormat form = new ChoiceFormat(limits, dayOfWeekNames);
 ParsePosition status = new ParsePosition(0);
 for (double i = 0.0; i <= 8.0; ++i) {
   status.setIndex(0);
   System.out.println(i + " -> " + form.format(i) + " -> "
               + form.parse(form.format(i),status));
 }
  
这是一个更复杂的示例,具有模式格式:

 double[] filelimits = {0,1,2};
 String[] filepart = {"are no files","is one file","are {2} files"};
 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
 Format[] testFormats = {fileform, null, NumberFormat.getInstance()};
 MessageFormat pattform = new MessageFormat("There {0} on {1}");
 pattform.setFormats(testFormats);
 Object[] testArgs = {null, "ADisk", null};
 for (int i = 0; i < 4; ++i) {
   testArgs[0] = Integer.valueOf(i);
   testArgs[2] = testArgs[0];
   System.out.println(pattform.format(testArgs));
 }
  

为 ChoiceFormat 对象指定模式非常简单。例如:


 ChoiceFormat fmt = new ChoiceFormat(
   "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+ |2#is two |2<is more than 2.");
 System.out.println("Formatter Pattern : " + fmt.toPattern());

 System.out.println("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY));
 System.out.println("Format with -1.0 : " + fmt.format(-1.0));
 System.out.println("Format with 0 : " + fmt.format(0));
 System.out.println("Format with 0.9 : " + fmt.format(0.9));
 System.out.println("Format with 1.0 : " + fmt.format(1));
 System.out.println("Format with 1.5 : " + fmt.format(1.5));
 System.out.println("Format with 2 : " + fmt.format(2));
 System.out.println("Format with 2.1 : " + fmt.format(2.1));
 System.out.println("Format with NaN : " + fmt.format(Double.NaN));
 System.out.println("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY));
  
输出结果如下:

 Format with -INF : is negative
 Format with -1.0 : is negative
 Format with 0 : is zero or fraction
 Format with 0.9 : is zero or fraction
 Format with 1.0 : is one
 Format with 1.5 : is 1+
 Format with 2 : is two
 Format with 2.1 : is more than 2.
 Format with NaN : is negative
 Format with +INF : is more than 2.
  

同步化

选择格式不同步。建议为每个线程创建单独的格式实例。如果多个线程同时访问一个格式,则必须在外部进行同步。

自从:
1.1
参见:
  • 构造方法详细信息

  • 方法详情

    • applyPattern

      public void applyPattern(String  newPattern)
      设置模式。
      参数:
      newPattern - 请参阅类说明。
      抛出:
      NullPointerException - 如果 newPatternnull
    • toPattern

      public String  toPattern()
      获取模式。
      返回:
      模式字符串
    • setChoices

      public void setChoices(double[] limits, String [] formats)
      设置要在格式化中使用的选项。
      参数:
      limits - 包含您要使用该格式解析的最高值,并且应按升序排序。格式化 X 时,选择 i,其中 limit[i] ≤ X < limit[i+1]。如果limit数组不是升序排列,格式化的结果会不正确。
      formats - 是您要用于每个限制的格式。它们可以是格式对象或字符串。使用对象 Y 格式化时,如果对象是 NumberFormat,则调用 ((NumberFormat) Y).format(X)。否则 Y.toString() 被调用。
      抛出:
      NullPointerException - 如果 limitsformatsnull
    • getLimits

      public double[] getLimits()
      获取在构造方法中传递的限制。
      返回:
      限制。
    • getFormats

      public Object [] getFormats()
      获取构造方法中传递的格式。
      返回:
      格式。
    • format

      public StringBuffer  format(long number, StringBuffer  toAppendTo, FieldPosition  status)
      格式专业化。该方法真正调用了format(double, StringBuffer, FieldPosition),因此支持的long的范围只等于double可以存储的范围。这永远不会成为实际限制。
      指定者:
      format 在类 NumberFormat
      参数:
      number - 要格式化的长数字
      toAppendTo - 要附加格式化文本的 StringBuffer
      status - 跟踪字段在返回字符串中的位置。例如,要在 Locale.US 区域设置中格式化数字 123456789,如果给定的 fieldPositionNumberFormat.INTEGER_FIELD ,则 fieldPosition 的开始索引和结束索引将分别设置为 0 和 11,用于输出字符串 123,456,789
      返回:
      格式化的 StringBuffer
      参见:
    • format

      public StringBuffer  format(double number, StringBuffer  toAppendTo, FieldPosition  status)
      返回带有格式化双精度的模式。
      指定者:
      format 在类 NumberFormat
      参数:
      number - 要格式化和替换的数字。
      toAppendTo - 附加文本的位置。
      status - 忽略不返回任何有用的状态。
      返回:
      格式化的 StringBuffer
      抛出:
      NullPointerException - 如果 toAppendTonull
      参见:
    • parse

      public Number  parse(String  text, ParsePosition  status)
      从输入文本中解析数字。
      指定者:
      parse 在类 NumberFormat
      参数:
      text - 源文本。
      status - 输入输出参数。输入时,status.index 字段指示应解析的源文本的第一个字符。退出时,如果没有发生错误,status.index 将设置为源文本中第一个未解析的字符。退出时,如果确实发生错误,则 status.index 不变,status.errorIndex 设置为导致解析失败的字符的第一个索引。
      返回:
      一个数字,表示已解析数字的值。
      抛出:
      NullPointerException - 如果 statusnull 或者如果 textnull 并且选择字符串列表不为空。
      参见:
    • nextDouble

      public static final double nextDouble(double d)
      找到大于 d 的最小双倍数。如果 NaN ,则返回相同的值。

      用于制作半开区间。

      实现注意事项:
      这相当于调用 Math.nextUp(d)
      参数:
      d——参考值
      返回:
      大于 d 的最小双精度值
      参见:
    • previousDouble

      public static final double previousDouble(double d)
      找到小于 d 的最大双数。如果 NaN ,则返回相同的值。
      实现注意事项:
      这相当于调用 Math.nextDown(d)
      参数:
      d——参考值
      返回:
      小于 d 的最大双精度值
      参见:
    • clone

      public Object  clone()
      重写可克隆
      重写:
      clone 在类 NumberFormat
      返回:
      此实例的克隆。
      参见:
    • hashCode

      public int hashCode()
      为消息格式对象生成哈希码。
      重写:
      hashCode 在类 NumberFormat
      返回:
      此对象的哈希码值。
      参见:
    • equals

      public boolean equals(Object  obj)
      两者相等比较
      重写:
      equals 在类 NumberFormat
      参数:
      obj - 要比较的参考对象。
      返回:
      true 如果此对象与 obj 参数相同; false否则。
      参见:
    • nextDouble

      public static double nextDouble(double d, boolean positive)
      查找大于 d 的最小双精度(如果 positivetrue ),或小于 d 的最大双精度(如果 positivefalse )。如果 NaN ,则返回相同的值。
      实现注意事项:
      这相当于调用 positive ? Math.nextUp(d) : Math.nextDown(d)
      参数:
      d——参考值
      positive - true 如果需要最小加倍; false否则
      返回:
      最小或更大的双精度值