java.lang.Object
|
+----java.text.Format
|
+----java.text.NumberFormat
|
+----java.text.ChoiceFormat
ChoiceFormat 允许您为一定范围的数附加格式。它通常用于 MessageFormat 中处理复数。
choice 用一个升序的双精度数表来指定,表中的每一项后有一个指向下一个项目的半开的间隔符:
如果没有匹配,则使用第一个还是最后一个索引取决于 number (X) 偏低还是偏高。X 与 j 匹配当且仅当 limit[j] <= x < limit[j+1]
注意:ChoiceFormat 与其它 Format 类不同,因为用构造子创建 ChoiceFormat 对象 (而不是用 getInstance 的式样工厂方法 )。
不需要这些工厂方法,因为 ChoiceFormat 不要求为给定的语言环境进行复杂的设置。实际上,ChoiceFormat 没有实现任何语言环境特定的行为。
当创建一个 ChoiceFormat 时, 必须指定一个格式数组和限制数组。这些数组长度必须一致。例如,
nextDouble 可用于获取下一个更高的双精度数,制做半开的间隔符。)
下面有一个例子显示了格式化和分析:
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)); }
public ChoiceFormat(String newPattern)
public ChoiceFormat(double limits[],
String formats[])
public void applyPattern(String newPattern)
public String toPattern()
public void setChoices(double limits[], String formats[])
public double[] getLimits()
public Object[] getFormats()
public StringBuffer format(long number,
StringBuffer toAppendTo,
FieldPosition status)
format(double, StringBuffer,
FieldPosition) ,因而被支持的 long 型的范围与可被 double 存储的范围相等。这永远都不会是一个实用的限制。
public StringBuffer format(double number,
StringBuffer toAppendTo,
FieldPosition status)
public Number parse(String text,
ParsePosition status)
public static final double nextDouble(double d)
用于做半开的间隔。
public static final double previousDouble(double d)
public Object clone()
public int hashCode()
public boolean equals(Object obj)
public static double nextDouble(double d,
boolean positive)