当前位置 类层次(JDK) 所有类 (JDK) 所有域和方法 (JDK)

类 java.text.ChoiceFormat

java.lang.Object
   |
   +----java.text.Format
           |
           +----java.text.NumberFormat
                   |
                   +----java.text.ChoiceFormat

public class ChoiceFormat
extends NumberFormat

ChoiceFormat 允许您为一定范围的数附加格式。它通常用于 MessageFormat 中处理复数。 choice 用一个升序的双精度数表来指定,表中的每一项后有一个指向下一个项目的半开的间隔符:

 X 与 j 匹配当且仅当 limit[j] <= x < limit[j+1] 
如果没有匹配,则使用第一个还是最后一个索引取决于 number (X) 偏低还是偏高。

注意:ChoiceFormat 与其它 Format 类不同,因为用构造子创建 ChoiceFormat 对象 (而不是用 getInstance 的式样工厂方法 )。 不需要这些工厂方法,因为 ChoiceFormat 不要求为给定的语言环境进行复杂的设置。实际上,ChoiceFormat 没有实现任何语言环境特定的行为。

当创建一个 ChoiceFormat 时, 必须指定一个格式数组和限制数组。这些数组长度必须一致。例如,

下面有一个例子显示了格式化和分析:

 double[] limits = {1,2,3,4,5,6,7};
 String[] monthNames = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
 ChoiceFormat form = new ChoiceFormat(limits, monthNames);
 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]="new" integer(i); testargs[2]="testArgs[0];" system.out.println(pattform.format(testargs)); } 

See Also:
DecimalFormat, MessageFormat

构造子索引

ChoiceFormat(double[], String[])
用限制和相应的格式构造。
ChoiceFormat(String)
用基于模式的限制和相应的格式构造。

方法索引

applyPattern(String)
设置模式。
clone()
覆盖 Cloneable。
equals(Object)
比较两个对象是否相等。
format(double, StringBuffer, FieldPosition)
格式的规范。
format(long, StringBuffer, FieldPosition)
格式的规范。
getFormats()
获取在构造子中传递的格式。
getLimits()
获取在构造子中传递的限制。
hashCode()
为消息格式对象生成一个散列码。
nextDouble(double)
查找大于 d 的最小双精度数。
nextDouble(double, boolean)
parse(String, ParsePosition)
如果可能的话,返回一个 Long 。
previousDouble(double)
查找小于 d 的最大双精度数。
setChoices(double[], String[])
设置用于格式化的 choice 。
toPattern()
获取模式。

构造子

ChoiceFormat
 public ChoiceFormat(String newPattern)
用基于模式的限制和相应的格式构造。

ChoiceFormat
 public ChoiceFormat(double limits[],
                     String formats[])
用限制和相应的格式构造。

参见:
setChoices

方法

applyPattern
 public void applyPattern(String newPattern)
设置模式。

参数:
newPattern - 参见类描述。
toPattern
 public String toPattern()
获取该模式。

setChoices
 public void setChoices(double limits[],
String formats[])
设置用于格式化的选项 。

参数:
limits - 包含您想用那个格式分析的最高值,并应以升序排列。当格式化 X 时, choice 是 i, limit[i]<= x < limit[i+1].
formats - 是您想为每一个限制使用的格式。它们可以是 Format 对象或 Strings 。当用对象 Y 格式化时, 如果对象是一个 NumberFormat, 则调用 ((NumberFormat) Y).format(X) 。否则调用 Y.toString() 。
getLimits
 public double[] getLimits()
获取在构造子中传递的限制。

返回值:
限制。
getFormats
 public Object[] getFormats()
获取在构造子中传递的格式。

返回值:
格式。
format
 public StringBuffer format(long number,
                            StringBuffer toAppendTo,
                            FieldPosition status)
格式的规范。该方法真正调用 format(double, StringBuffer, FieldPosition) ,因而被支持的 long 型的范围与可被 double 存储的范围相等。这永远都不会是一个实用的限制。

覆盖:
NumberFormat 中的 format
format
 public StringBuffer format(double number,
                            StringBuffer toAppendTo,
                            FieldPosition status)
格式的规范。

覆盖:
NumberFormat 中的 format
parse
 public Number parse(String text,
                     ParsePosition status)
如果可能的话,返回一个 Long (e.g.

覆盖:
NumberFormat 中的 parse
nextDouble
 public static final double nextDouble(double d)
查找大于 d 的最小双精度数。 如果是 NaN, 返回同样的值。

用于做半开的间隔。

参见:
previousDouble
previousDouble
 public static final double previousDouble(double d)
查找小于 d 的最大双精度数。 如果是 NaN, 返回同样的值。

参见:
nextDouble
clone
 public Object clone()
覆盖 Cloneable

覆盖:
NumberFormat 中的 clone
hashCode
 public int hashCode()
为消息格式对象生成一个散列码。

覆盖:
NumberFormat 中的 hashCodeequals
equals
 public boolean equals(Object obj)
比较两个对象是否相等

覆盖:
NumberFormat 中的 equals
nextDouble
 public static double nextDouble(double d,
                                 boolean positive)

当前位置 类层次(JDK) 所有类 (JDK) 所有域和方法 (JDK)