Most visited

Recently visited

Added in API level 1

ChoiceFormat

public class ChoiceFormat
extends NumberFormat

java.lang.Object
   ↳ java.text.Format
     ↳ java.text.NumberFormat
       ↳ java.text.ChoiceFormat


A ChoiceFormat允许您将格式附加到一系列数字。 它通常用于处理复数的MessageFormat 选项用双列表的升序列表指定,每个项目指定直到下一个项目的半开间隔:

 X matches j if and only if limit[j] <= X < limit[j+1]
 
If there is no match, then either the first or last index is used, depending on whether the number (X) is too low or too high. If the limit array is not in ascending order, the results of formatting will be incorrect. ChoiceFormat also accepts \u221E as equivalent to infinity(INF).

注意: ChoiceFormat不同于其他Format类,因为您使用构造函数创建了ChoiceFormat对象(不带getInstance样式工厂方法)。 工厂方法不是必需的,因为ChoiceFormat不需要针对给定语言环境的任何复杂设置。 实际上, ChoiceFormat并未实现任何特定于语言环境的行为。

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

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

 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));
 }
 
Here is a more complex example, with a pattern format:
 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));
 }
 

指定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));
 
And the output result would be like the following:
   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.
 

Synchronization

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

也可以看看:

Summary

Inherited constants

From class java.text.NumberFormat

Public constructors

ChoiceFormat(String newPattern)

根据模式构建限制和相应的格式。

ChoiceFormat(double[] limits, String[] formats)

用限制和相应的格式构造。

Public methods

void applyPattern(String newPattern)

设置模式。

Object clone()

覆盖可复制

boolean equals(Object obj)

两者之间的平等比较

StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition status)

用格式化的double返回模式。

StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition status)

格式专业化。

Object[] getFormats()

获取构造函数中传递的格式。

double[] getLimits()

获取构造函数中传递的限制。

int hashCode()

为消息格式对象生成哈希码。

static final double nextDouble(double d)

发现至少比d大一倍。

static double nextDouble(double d, boolean positive)

查找至少大于d的double(如果positive == true),或者最大double小于d(如果positive == false)。

Number parse(String text, ParsePosition status)

从输入文本中解析数字。

static final double previousDouble(double d)

找到比d更小的最大双倍数。

void setChoices(double[] limits, String[] formats)

设置要在格式化中使用的选项。

String toPattern()

获取模式。

Inherited methods

From class java.text.NumberFormat
From class java.text.Format
From class java.lang.Object

Public constructors

ChoiceFormat

Added in API level 1
ChoiceFormat (String newPattern)

根据模式构建限制和相应的格式。

Parameters
newPattern String

也可以看看:

ChoiceFormat

Added in API level 1
ChoiceFormat (double[] limits, 
                String[] formats)

用限制和相应的格式构造。

Parameters
limits double
formats String

也可以看看:

Public methods

applyPattern

Added in API level 1
void applyPattern (String newPattern)

设置模式。

Parameters
newPattern String: See the class description.

clone

Added in API level 1
Object clone ()

覆盖可复制

Returns
Object a clone of this instance.

equals

Added in API level 1
boolean equals (Object obj)

两者之间的平等比较

Parameters
obj Object: the reference object with which to compare.
Returns
boolean true if this object is the same as the obj argument; false otherwise.

format

Added in API level 1
StringBuffer format (double number, 
                StringBuffer toAppendTo, 
                FieldPosition status)

用格式化的double返回模式。

Parameters
number double: number to be formatted & substituted.
toAppendTo StringBuffer: where text is appended.
status FieldPosition: ignore no useful status is returned.
Returns
StringBuffer

format

Added in API level 1
StringBuffer format (long number, 
                StringBuffer toAppendTo, 
                FieldPosition status)

格式专业化。 该方法真的调用format(double, StringBuffer, FieldPosition)因此支持的long的范围仅等于可以通过double存储的范围。 这绝不是一个实际的限制。

Parameters
number long
toAppendTo StringBuffer
status FieldPosition
Returns
StringBuffer

getFormats

Added in API level 1
Object[] getFormats ()

获取构造函数中传递的格式。

Returns
Object[] the formats.

getLimits

Added in API level 1
double[] getLimits ()

获取构造函数中传递的限制。

Returns
double[] the limits.

hashCode

Added in API level 1
int hashCode ()

为消息格式对象生成哈希码。

Returns
int a hash code value for this object.

nextDouble

Added in API level 1
double nextDouble (double d)

发现至少比d大一倍。 如果NaN返回相同的值。

用于制作半开间隔。

Parameters
d double
Returns
double

也可以看看:

nextDouble

Added in API level 1
double nextDouble (double d, 
                boolean positive)

查找至少大于d的double(如果positive == true),或者最大double小于d(如果positive == false)。 如果NaN返回相同的值。 如果这些成员函数不这样做,则不影响浮点标志:Double.longBitsToDouble(long)Double.doubleToLongBits(double)Double.isNaN(double)

Parameters
d double
positive boolean
Returns
double

parse

Added in API level 1
Number parse (String text, 
                ParsePosition status)

从输入文本中解析数字。

Parameters
text String: the source text.
status ParsePosition: an input-output parameter. On input, the status.index field indicates the first character of the source text that should be parsed. On exit, if no error occured, status.index is set to the first unparsed character in the source text. On exit, if an error did occur, status.index is unchanged and status.errorIndex is set to the first index of the character that caused the parse to fail.
Returns
Number A Number representing the value of the number parsed.

previousDouble

Added in API level 1
double previousDouble (double d)

找到比d更小的最大双倍数。 如果NaN返回相同的值。

Parameters
d double
Returns
double

也可以看看:

setChoices

Added in API level 1
void setChoices (double[] limits, 
                String[] formats)

设置要在格式化中使用的选项。

Parameters
limits double: contains the top value that you want parsed with that format,and should be in ascending sorted order. When formatting X, the choice will be the i, where limit[i] <= X < limit[i+1]. If the limit array is not in ascending order, the results of formatting will be incorrect.
formats String: are the formats you want to use for each limit. They can be either Format objects or Strings. When formatting with object Y, if the object is a NumberFormat, then ((NumberFormat) Y).format(X) is called. Otherwise Y.toString() is called.

toPattern

Added in API level 1
String toPattern ()

获取模式。

Returns
String

Hooray!