public class ChoiceFormat extends NumberFormat
ChoiceFormat
允许你附加格式的数字范围。它一般是用在一个
MessageFormat
处理复数。选择与提升的单双打的规定,每个项目指定一个半开区间上的下一个项目:
如果没有比赛,那么无论是第一个或最后一个指标是,取决于数(x)太低或太高。如果限制数组不是在上升的顺序,格式的结果将是不正确的。choiceformat也接受X matches j if and only if limit[j] ≤ X < limit[j+1]
\u221E
等同于无限(INF)。
注: ChoiceFormat
不同于其他Format
类在你创建一个构造函数ChoiceFormat
对象(不是一个getInstance
风格的工厂方法)。工厂方法并不需要,因为ChoiceFormat
不需要任何复杂的设置,对于一个给定的语言环境。事实上,ChoiceFormat
不执行任何特定场所的行为。
创建一个ChoiceFormat
时,您必须指定格式的数组和数组的界限。这些数组的长度必须是相同的。例如,
nextDouble
可得到更高一倍,使半开区间。)这里是一个简单的例子,显示格式和解析:
这里是一个更复杂的例子,一个图形格式: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] = 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));
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.
选择格式不同步。建议为每个线程创建单独的格式实例。如果多个线程同时访问一个格式,则必须在外部同步。
NumberFormat.Field
FRACTION_FIELD, INTEGER_FIELD
Constructor and Description |
---|
ChoiceFormat(double[] limits, String[] formats)
结构的限制和相应的格式。
|
ChoiceFormat(String newPattern)
基于模式的限制和相应格式的构造。
|
Modifier and Type | Method and Description |
---|---|
void |
applyPattern(String newPattern)
设置模式。
|
Object |
clone()
覆盖可复制
|
boolean |
equals(Object obj)
两个等式的比较
|
StringBuffer |
format(double number, StringBuffer toAppendTo, FieldPosition status)
带格式化的双返回模式。
|
StringBuffer |
format(long number, StringBuffer toAppendTo, FieldPosition status)
格式专业化。
|
Object[] |
getFormats()
获取构造函数中传递的格式。
|
double[] |
getLimits()
获取构造函数中传递的限制。
|
int |
hashCode()
为消息格式对象生成一个哈希代码。
|
static double |
nextDouble(double d)
发现至少大于
d 。
|
static double |
nextDouble(double d, boolean positive)
发现至少大于
d (如果
positive 是
true ),或最大的双小于
d (如果
positive 是
false )。
|
Number |
parse(String text, ParsePosition status)
解析数字输入文本。
|
static double |
previousDouble(double d)
发现最大的双小于
d 。
|
void |
setChoices(double[] limits, String[] formats)
设置格式化中使用的选项。
|
String |
toPattern()
获取模式。
|
format, format, format, getAvailableLocales, 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
format, formatToCharacterIterator, parseObject
public ChoiceFormat(String newPattern)
newPattern
-新模式字符串
applyPattern(java.lang.String)
public ChoiceFormat(double[] limits, String[] formats)
limits
限制升序
formats
相应格式字符串
setChoices(double[], java.lang.String[])
public void applyPattern(String newPattern)
newPattern
看到类的描述。
public String toPattern()
public void setChoices(double[] limits, String[] formats)
limits
-包含要解析格式的最高价值,并应在升序排序。当格式化X,选择是的我,在极限[我]≤x<极限[我+ 1 ]。如果限制数组不是在上升的顺序,格式的结果将是不正确的。
formats
-你想使用每个限制格式。它们可以是格式化对象或字符串。当格式化对象,如果对象是一个解析,然后((解析)y)。格式(x)称为。否则tostring()叫Y.。
public double[] getLimits()
public Object[] getFormats()
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition status)
format(double, StringBuffer, FieldPosition)
这样范围的渴望,支持只相当于范围可以存储由双。这将永远不会是一个实际的限制。
format
方法重写,继承类
NumberFormat
number
-长的数字格式
toAppendTo
的StringBuffer的格式化的文本要附加
status
-场上位置
Format.format(java.lang.Object)
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition status)
format
方法重写,继承类
NumberFormat
number
号码被格式化和取代。
toAppendTo
-文本追加。
status
忽略任何有用的状态返回。
Format.format(java.lang.Object)
public Number parse(String text, ParsePosition status)
parse
方法重写,继承类
NumberFormat
text
-原文。
status
-输入输出参数。对输入的status.index指出原文应该解析的第一个字符。在退出时,如果没有错误发生,status.index将源文本中的第一个分析的特点。退出的时候,如果真的发生了一个错误,status.index是不变的,status.errorindex将导致解析失败的字符的第一个指标。
NumberFormat.isParseIntegerOnly()
,
Format.parseObject(java.lang.String, java.text.ParsePosition)
public static final double nextDouble(double d)
d
。如果
NaN
,返回相同的值。
用来使半开的时间间隔。
d
-参考值
d
previousDouble(double)
public static final double previousDouble(double d)
d
。如果
NaN
,返回相同的值。
d
-参考值
d
nextDouble(double)
public Object clone()
clone
方法重写,继承类
NumberFormat
Cloneable
public int hashCode()
hashCode
方法重写,继承类
NumberFormat
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public boolean equals(Object obj)
equals
方法重写,继承类
NumberFormat
obj
-参考对象的比较。
true
obj参数相同;
false
否则。
Object.hashCode()
,
HashMap
public static double nextDouble(double d, boolean positive)
d
(如果
positive
是
true
),或最大的双小于
d
(如果
positive
是
false
)。如果
NaN
,返回相同的值。不影响浮点旗帜,提供这些成员函数不:双。longbitstodouble(长)双(双)。doubletolongbits双isnan(双)。
d
-参考值
positive
-
true
如果至少需要
false
否则;
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.