java.lang.Object
java.text.Format
java.text.NumberFormat
java.text.ChoiceFormat
- 所有已实现的接口:
Serializable,Cloneable
ChoiceFormat 允许您将格式附加到一系列数字。它通常在MessageFormat 中用于处理复数。选择是用双精度的升序列表指定的,其中每个项目指定到下一个项目的半开间隔:
如果没有匹配项,则使用第一个或最后一个索引,具体取决于数字 (X) 是太低还是太高。如果limit数组不是升序排列,格式化的结果会不正确。 ChoiceFormat 还接受X matches j if and only if limit[j] ≤ X < limit[j+1]
\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
- 参见:
-
内部类总结
在类 java.text.NumberFormat 中声明的嵌套类/接口
NumberFormat.Field, NumberFormat.Style -
字段摘要
在类 java.text.NumberFormat 中声明的字段
FRACTION_FIELD, INTEGER_FIELD -
构造方法总结
构造方法构造方法描述ChoiceFormat(double[] limits, String[] formats) 用限制和相应的格式构造。ChoiceFormat(String newPattern) 根据模式构建具有限制和相应格式的结构。 -
方法总结
修饰符和类型方法描述voidapplyPattern(String newPattern) 设置模式。clone()重写可克隆boolean两者相等比较format(double number, StringBuffer toAppendTo, FieldPosition status) 返回带有格式化双精度的模式。format(long number, StringBuffer toAppendTo, FieldPosition status) 格式专业化。Object[]获取构造方法中传递的格式。double[]获取在构造方法中传递的限制。inthashCode()为消息格式对象生成哈希码。static final doublenextDouble(double d) 找到大于d的最小双倍数。static doublenextDouble(double d, boolean positive) 查找大于d的最小双精度(如果positive为true),或小于d的最大双精度(如果positive为false)。parse(String text, ParsePosition status) 从输入文本中解析数字。static final doublepreviousDouble(double d) 找到小于d的最大双数。voidsetChoices(double[] limits, String[] formats) 设置要在格式化中使用的选项。获取模式。在类 java.text.NumberFormat 中声明的方法
format, format, format, getAvailableLocales, getCompactNumberInstance, getCompactNumberInstance, getCurrency, getCurrencyInstance, getCurrencyInstance, getInstance, getInstance, getIntegerInstance, getIntegerInstance, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getNumberInstance, getNumberInstance, getPercentInstance, getPercentInstance, getRoundingMode, isGroupingUsed, isParseIntegerOnly, parse, parseObject, setCurrency, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly, setRoundingMode在类 java.text.Format 中声明的方法
format, formatToCharacterIterator, parseObject
-
构造方法详细信息
-
ChoiceFormat
根据模式构建具有限制和相应格式的结构。- 参数:
newPattern- 新模式字符串- 抛出:
NullPointerException- 如果newPattern是null- 参见:
-
ChoiceFormat
用限制和相应的格式构造。- 参数:
limits- 升序限制formats- 相应的格式字符串- 抛出:
NullPointerException- 如果limits或formats是null- 参见:
-
-
方法详情
-
applyPattern
设置模式。- 参数:
newPattern- 请参阅类说明。- 抛出:
NullPointerException- 如果newPattern是null
-
toPattern
获取模式。- 返回:
- 模式字符串
-
setChoices
设置要在格式化中使用的选项。- 参数:
limits- 包含您要使用该格式解析的最高值,并且应按升序排序。格式化 X 时,选择 i,其中 limit[i] ≤ X < limit[i+1]。如果limit数组不是升序排列,格式化的结果会不正确。formats- 是您要用于每个限制的格式。它们可以是格式对象或字符串。使用对象 Y 格式化时,如果对象是 NumberFormat,则调用 ((NumberFormat) Y).format(X)。否则 Y.toString() 被调用。- 抛出:
NullPointerException- 如果limits或formats是null
-
getLimits
public double[] getLimits()获取在构造方法中传递的限制。- 返回:
- 限制。
-
getFormats
获取构造方法中传递的格式。- 返回:
- 格式。
-
format
格式专业化。该方法真正调用了format(double, StringBuffer, FieldPosition),因此支持的long的范围只等于double可以存储的范围。这永远不会成为实际限制。- 指定者:
format在类NumberFormat中- 参数:
number- 要格式化的长数字toAppendTo- 要附加格式化文本的 StringBufferstatus- 跟踪字段在返回字符串中的位置。例如,要在Locale.US区域设置中格式化数字123456789,如果给定的fieldPosition是NumberFormat.INTEGER_FIELD,则fieldPosition的开始索引和结束索引将分别设置为 0 和 11,用于输出字符串123,456,789。- 返回:
- 格式化的 StringBuffer
- 参见:
-
format
返回带有格式化双精度的模式。- 指定者:
format在类NumberFormat中- 参数:
number- 要格式化和替换的数字。toAppendTo- 附加文本的位置。status- 忽略不返回任何有用的状态。- 返回:
- 格式化的 StringBuffer
- 抛出:
NullPointerException- 如果toAppendTo是null- 参见:
-
parse
从输入文本中解析数字。- 指定者:
parse在类NumberFormat中- 参数:
text- 源文本。status- 输入输出参数。输入时,status.index 字段指示应解析的源文本的第一个字符。退出时,如果没有发生错误,status.index 将设置为源文本中第一个未解析的字符。退出时,如果确实发生错误,则 status.index 不变,status.errorIndex 设置为导致解析失败的字符的第一个索引。- 返回:
- 一个数字,表示已解析数字的值。
- 抛出:
NullPointerException- 如果status是null或者如果text是null并且选择字符串列表不为空。- 参见:
-
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
重写可克隆- 重写:
clone在类NumberFormat中- 返回:
- 此实例的克隆。
- 参见:
-
hashCode
public int hashCode()为消息格式对象生成哈希码。- 重写:
hashCode在类NumberFormat中- 返回:
- 此对象的哈希码值。
- 参见:
-
equals
两者相等比较- 重写:
equals在类NumberFormat中- 参数:
obj- 要比较的参考对象。- 返回:
true如果此对象与 obj 参数相同;false否则。- 参见:
-
nextDouble
public static double nextDouble(double d, boolean positive) 查找大于d的最小双精度(如果positive为true),或小于d的最大双精度(如果positive为false)。如果NaN,则返回相同的值。- 实现注意事项:
-
这相当于调用
positive ? Math.nextUp(d) : Math.nextDown(d) - 参数:
d——参考值positive-true如果需要最小加倍;false否则- 返回:
- 最小或更大的双精度值
-