decimalformat.html

来自「API資料大全」· HTML 代码 · 共 1,215 行 · 第 1/5 页

HTML
1,215
字号
<!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:38 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class  DecimalFormat</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/DecimalFormat.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/DateFormatSymbols.html"><B>PREV CLASS</B></A>&nbsp;&nbsp;<A HREF="../../java/text/DecimalFormatSymbols.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="DecimalFormat.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  DecimalFormat</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.DecimalFormat</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>DecimalFormat</B><DT>extends <A HREF="../../java/text/NumberFormat.html">NumberFormat</A></DL><P><code>DecimalFormat</code> is a concrete subclass of <code>NumberFormat</code> that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits.  It also supports different kinds of numbers, including integers (123), fixed-point numbers (123.4), scientific notation (1.23E4), percentages (12%), and currency amounts ($123).  All of these can be localized. <p>To obtain a <code>NumberFormat</code> for a specific locale, including the default locale, call one of <code>NumberFormat</code>'s factory methods, such as <code>getInstance()</code>.  In general, do not call the <code>DecimalFormat</code> constructors directly, since the <code>NumberFormat</code> factory methods may return subclasses other than <code>DecimalFormat</code>. If you need to customize the format object, do something like this: <blockquote><pre> NumberFormat f = NumberFormat.getInstance(loc); if (f instanceof DecimalFormat) {     ((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true); } </pre></blockquote> <p>A <code>DecimalFormat</code> comprises a <em>pattern</em> and a set of <em>symbols</em>.  The pattern may be set directly using <code>applyPattern()</code>, or indirectly using the API methods.  The symbols are stored in a <code>DecimalFormatSymbols</code> object.  When using the <code>NumberFormat</code> factory methods, the pattern and symbols are read from localized <code>ResourceBundle</code>s in the package <code>java.text.resource</code>. <p><strong>Example</strong> <blockquote><pre> <strong>// Print out a number using the localized number, currency, // and percent format for each locale</strong> Locale[] locales = NumberFormat.getAvailableLocales(); double myNumber = -1234.56; NumberFormat form; for (int j=0; j<3; ++j) {     System.out.println("FORMAT");     for (int i = 0; i < locales.length; ++i) {         if (locales[i].getCountry().length() == 0) {            continue; // Skip language-only locales         }         System.out.print(locales[i].getDisplayName());         switch (j) {         case 0:             form = NumberFormat.getInstance(locales[i]); break;         case 1:             form = NumberFormat.getCurrencyInstance(locales[i]); break;         default:             form = NumberFormat.getPercentInstance(locales[i]); break;         }         try {             // Assume form is a DecimalFormat             System.out.print(": " + ((DecimalFormat) form).toPattern()                              + " -> " + form.format(myNumber));         } catch (IllegalArgumentException e) {}         try {             System.out.println(" -> " + form.parse(form.format(myNumber)));         } catch (ParseException e) {}     } } </pre></blockquote> <p><strong>Patterns</strong> <p>A <code>DecimalFormat</code> pattern contains a postive and negative subpattern, for example, "#,##0.00;(#,##0.00)".  Each subpattern has a prefix, numeric part, and suffix.  The negative subpattern is optional; if absent, then the positive subpattern prefixed with the localized minus sign ('-' in most locales) is used as the negative subpattern. That is, "0.00" alone is equivalent to "0.00;-0.00".  If there is an explicit negative subpattern, it serves only to specify the negative prefix and suffix; the number of digits, minimal digits, and other characteristics are all the same as the positive pattern. That means that "#,##0.0#;(#)" produces precisely the same behavior as "#,##0.0#;(#,##0.0#)". <p>The prefixes, suffixes, and various symbols used for infinity, digits, thousands separators, decimal separators, etc. may be set to arbitrary values, and they will appear properly during formatting.  However, care must be taken that the symbols and strings do not conflict, or parsing will be unreliable.  For example, either the positive and negative prefixes or the suffixes must be distinct for <code>DecimalFormat.parse()</code> to be able to distinguish positive from negative values.  (If they are identical, then <code>DecimalFormat</code> will behave as if no negative subpattern was specified.)  Another example is that the decimal separator and thousands separator should be distinct characters, or parsing will be impossible. <p>The grouping separator is commonly used for thousands, but in some countries it separates ten-thousands. The grouping size is a constant number of digits between the grouping characters, such as 3 for 100,000,000 or 4 for 1,0000,0000.  If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####". <p>Illegal patterns, such as "#.#.#" or "#.###,###", will cause <code>DecimalFormat</code> to throw an <code>IllegalArgumentException</code> with a message that describes the problem. <p><strong>Parsing</strong> <p><code>DecimalFormat</code> parses all Unicode characters that represent decimal digits, as defined by <code>Character.digit()</code>.  In addition, <code>DecimalFormat</code> also recognizes as digits the ten consecutive characters starting with the localized zero digit defined in the <code>DecimalFormatSymbols</code> object.  During formatting, the <code>DecimalFormatSymbols</code>-based digits are output. <p><code>DecimalFormat.parse</code> returns a subclass of <code>java.lang.Number</code> representing the parsed numeric string. <code>DecimalFormat</code> chooses the most economical subclass that can represent the numeric string.  This means most integer values are returned as <code>Long</code> objects, no matter how they are written: "17" and "17.000" both parse to <code>Long(17)</code>.  Values that cannot fit into a <code>Long</code> are returned as <code>Double</code>s.  This includes values with a fractional part, infinite values, <code>NaN</code>, and the value -0.0.  <code>DecimalFormat</code> does <em>not</em> decide whether to return a <code>Double</code> or a <code>Long</code> based on the presence of a decimal separator in the source string.  Doing so would prevent integers that overflow the mantissa of a double, such as "10,000,000,000,000,000.00", from being parsed accurately.  Currently, the only classes that <code>DecimalFormat</code> returns are <code>Long</code> and <code>Double</code>, but callers should not rely on this.  Callers may use the <code>Number</code> methods <code>doubleValue</code>, <code>longValue</code>, etc., to obtain the type they want. <p>If <code>DecimalFormat.parse(String, ParsePosition)</code> fails to parse a string, it returns <code>null</code>, leaves the <code>ParsePosition</code> index unchanged, and sets the <code>ParsePosition</code> error index.  The convenience method <code>DecimalFormat.parse(String)</code> indicates parse failure by throwing a <code>ParseException</code>. <p><strong>Special Values</strong> <p><code>NaN</code> is formatted as a single character, typically <code>&#92;uFFFD</code>.  This character is determined by the <code>DecimalFormatSymbols</code> object.  This is the only value for which the prefixes and suffixes are not used. <p>Infinity is formatted as a single character, typically <code>&#92;u221E</code>, with the positive or negative prefixes and suffixes applied.  The infinity character is determined by the <code>DecimalFormatSymbols</code> object. <p>Negative zero ("-0") parses to <code>Double(-0.0)</code>, unless <code>isParseIntegerOnly()</code> is true, in which case it parses to <code>Long(0)</code>. <p> <strong>Scientific Notation</strong> <p>Numbers in scientific notation are expressed as the product of a mantissa and a power of ten, for example, 1234 can be expressed as 1.234 x 10^3.  The mantissa is often in the range 1.0 <= x < 10.0, but it need not be. <code>DecimalFormat</code> can be instructed to format and parse scientific notation <em>only via a pattern</em>; there is currently no factory method that creates a scientific notation format.  In a pattern, the exponent character immediately followed by one or more digit characters indicates scientific notation.  Example: "0.###E0" formats the number 1234 as "1.234E3". <ul> <li>The number of digit characters after the exponent character gives the minimum exponent digit count.  There is no maximum.  Negative exponents are

⌨️ 快捷键说明

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