choiceformat.html

来自「API資料大全」· HTML 代码 · 共 690 行 · 第 1/3 页

HTML
690
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:36:36 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class  ChoiceFormat</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3">  <TR ALIGN="center" VALIGN="top">  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="class-use/ChoiceFormat.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>  </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup>&nbsp;2&nbsp;Platform<br>Std.&nbsp;Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">&nbsp;<A HREF="../../java/text/BreakIterator.html"><B>PREV CLASS</B></A>&nbsp;&nbsp;<A HREF="../../java/text/CollationElementIterator.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">  <A HREF="../../index.html" TARGET="_top"><B>FRAMES</B></A>  &nbsp;&nbsp;<A HREF="ChoiceFormat.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">  SUMMARY: &nbsp;INNER&nbsp;|&nbsp;<A HREF="#fields_inherited_from_class_java.text.NumberFormat">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: &nbsp;FIELD&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.text</FONT><BR>Class  ChoiceFormat</H2><PRE><A HREF="../../java/lang/Object.html">java.lang.Object</A>  |  +--<A HREF="../../java/text/Format.html">java.text.Format</A>        |        +--<A HREF="../../java/text/NumberFormat.html">java.text.NumberFormat</A>              |              +--<B>java.text.ChoiceFormat</B></PRE><DL><DT><B>All Implemented Interfaces:</B> <DD><A HREF="../../java/lang/Cloneable.html">Cloneable</A>, <A HREF="../../java/io/Serializable.html">Serializable</A></DD></DL><HR><DL><DT>public class <B>ChoiceFormat</B><DT>extends <A HREF="../../java/text/NumberFormat.html">NumberFormat</A></DL><P>A <code>ChoiceFormat</code> allows you to attach a format to a range of numbers. It is generally used in a <code>MessageFormat</code> for handling plurals. The choice is specified with an ascending list of doubles, where each item specifies a half-open interval up to the next item: <blockquote> <pre> X matches j if and only if limit[j] &lt;= X &lt; limit[j+1] </pre> </blockquote> 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 <code>\\u221E</code> as equivalent to infinity(INF). <p> <strong>Note:</strong> <code>ChoiceFormat</code> differs from the other <code>Format</code> classes in that you create a <code>ChoiceFormat</code> object with a constructor (not with a <code>getInstance</code> style factory method). The factory methods aren't necessary because <code>ChoiceFormat</code> doesn't require any complex setup for a given locale. In fact, <code>ChoiceFormat</code> doesn't implement any locale specific behavior. <p> When creating a <code>ChoiceFormat</code>, you must specify an array of formats and an array of limits. The length of these arrays must be the same. For example, <ul> <li>     <em>limits</em> = {1,2,3,4,5,6,7}<br>     <em>formats</em> = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"} <li>     <em>limits</em> = {0, 1, ChoiceFormat.nextDouble(1)}<br>     <em>formats</em> = {"no files", "one file", "many files"}<br>     (<code>nextDouble</code> can be used to get the next higher double, to     make the half-open interval.) </ul> <p> Here is a simple example that shows formatting and parsing: <blockquote> <pre> 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 &lt;= 8.0; ++i) {     status.setIndex(0);     System.out.println(i + " -&gt; " + form.format(i) + " -&gt; "                              + form.parse(form.format(i),status)); } </pre> </blockquote> Here is a more complex example, with a pattern format: <blockquote> <pre> 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 &lt; 4; ++i) {     testArgs[0] = new Integer(i);     testArgs[2] = testArgs[0];     System.out.println(pattform.format(testArgs)); } </pre> </blockquote> <p> Specifying a pattern for ChoiceFormat objects is fairly straightforward. For example: <blockquote> <pre> ChoiceFormat fmt = new ChoiceFormat(      "-1#is negative| 0#is zero or fraction | 1#is one |1.0&lt;is 1+ |2#is two |2&lt;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)); </pre> </blockquote> And the output result would be like the following: <pre> <blockquote>   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. </pre> </blockquote><P><DL><DT><B>See Also: </B><DD><A HREF="../../java/text/DecimalFormat.html"><CODE>DecimalFormat</CODE></A>, <A HREF="../../java/text/MessageFormat.html"><CODE>MessageFormat</CODE></A>, <A HREF="../../serialized-form.html#java.text.ChoiceFormat">Serialized Form</A></DL><HR><P><!-- ======== INNER CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><A NAME="fields_inherited_from_class_java.text.NumberFormat"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor"><TD><B>Fields inherited from class java.text.<A HREF="../../java/text/NumberFormat.html">NumberFormat</A></B></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><A HREF="../../java/text/NumberFormat.html#FRACTION_FIELD">FRACTION_FIELD</A>, <A HREF="../../java/text/NumberFormat.html#INTEGER_FIELD">INTEGER_FIELD</A></CODE></TD></TR></TABLE>&nbsp;<!-- ======== CONSTRUCTOR SUMMARY ======== --><A NAME="constructor_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Constructor Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/text/ChoiceFormat.html#ChoiceFormat(double[], java.lang.String[])">ChoiceFormat</A></B>(double[]&nbsp;limits,             <A HREF="../../java/lang/String.html">String</A>[]&nbsp;formats)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructs with the limits and the corresponding formats.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><B><A HREF="../../java/text/ChoiceFormat.html#ChoiceFormat(java.lang.String)">ChoiceFormat</A></B>(<A HREF="../../java/lang/String.html">String</A>&nbsp;newPattern)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructs with limits and corresponding formats based on the pattern.</TD></TR></TABLE>&nbsp;<!-- ========== METHOD SUMMARY =========== -->

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?