double.html

来自「j2se5-api-zh,java文档的中文版本」· HTML 代码 · 共 1,256 行 · 第 1/5 页

HTML
1,256 行
字号
<DT><B>从以下版本开始:</B></DT>  <DD>JDK1.1</DD></DL></DL><!-- ========= CONSTRUCTOR DETAIL ======== --><A NAME="constructor_detail"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2"><B>构造方法详细信息</B></FONT></TH></TR></TABLE><A NAME="Double(double)"><!-- --></A><H3>Double</H3><PRE>public <B>Double</B>(double&nbsp;value)</PRE><DL><DD>构造一个新分配的 <code>Double</code> 对象,它表示基本的 <code>double</code> 参数。<P><DL><DT><B>参数:</B><DD><CODE>value</CODE> - 用 <code>Double</code> 表示的值。</DL></DL><HR><A NAME="Double(java.lang.String)"><!-- --></A><H3>Double</H3><PRE>public <B>Double</B>(<A HREF="String.html" title="java.lang 中的类">String</A>&nbsp;s)       throws <A HREF="NumberFormatException.html" title="java.lang 中的类">NumberFormatException</A></PRE><DL><DD>构造一个新分配的 <code>Double</code> 对象,表示用字符串表示的 <code>double</code> 类型的浮点值。该方法与 <code>valueOf</code> 方法类似,可将字符串转换为 <code>double</code> 值。<P><DL><DT><B>参数:</B><DD><CODE>s</CODE> - 要转换为 <code>Double</code> 的字符串。<DT><B>抛出:</B><DD><CODE><A HREF="NumberFormatException.html" title="java.lang 中的类">NumberFormatException</A></CODE> - 如果字符串不包含可分析的数字。<DT><B>另请参见:</B><DD><A HREF="Double.html#valueOf(java.lang.String)"><CODE>valueOf(java.lang.String)</CODE></A></DL></DL><!-- ============ METHOD DETAIL ========== --><A NAME="method_detail"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2"><B>方法详细信息</B></FONT></TH></TR></TABLE><A NAME="toString(double)"><!-- --></A><H3>toString</H3><PRE>public static <A HREF="String.html" title="java.lang 中的类">String</A> <B>toString</B>(double&nbsp;d)</PRE><DL><DD>返回 <code>double</code> 参数的字符串表示形式。下面提到的所有字符都是 ASCII 字符。 <ul><li>如果参数为 NaN,则结果是字符串 &quot;<code>NaN</code>&quot;。<li>否则,结果是表示参数的符号和大小(绝对值)的字符串。如果符号为负,则结果的第一个字符是 '<code>-</code>' (<code>'\u002D'</code>);如果符号为正,则结果中不显示符号字符。至于大小值 <i>m</i>: <ul><li>如果 <i>m</i> 为无穷大,则用字符 <code>&quot;Infinity&quot;</code> 表示它;因此,正无穷大产生的结果就是 <code>&quot;Infinity&quot;</code>,而负无穷大产生的结果是 <code>&quot;-Infinity&quot;</code>。<li>如果 <i>m</i> 为零,则用字符 <code>&quot;0.0&quot;</code> 表示它;因此,负零产生的结果是 <code>&quot;-0.0&quot;</code>,而正零产生的结果是 <code>&quot;0.0&quot;</code>。<li>如果 <i>m</i> 大于或者等于 10<sup>-3</sup>,但小于 10<sup>7</sup>,则采用不带前导零的十进制形式,用 <i>m</i> 的整数部分表示它,后面跟着 '<code>.</code>' (<code>'\u002E'</code>),再后面是表示 <i>m</i> 的小数部分的一个或多个十进制位数。<li>如果 <i>m</i> 小于 10<sup>-3</sup> 或者大于或等于 10<sup>7</sup>,则用所谓的&ldquo;计算机科学记数法&rdquo;表示它。设 <i>n</i> 是满足 10<sup><i>n</i></sup> &lt;= <i>m</i> &lt; 10<sup><i>n</i>+1</sup> 的惟一整数;然后设 <i>a</i> 是 <i>m</i> 与 10<sup><i>n</i></sup> 的精确算术商数值,并满足 1 &lt;= <i>a</i> &lt; 10。则值 m 用 <i>a</i> 的整数部分表示大小值(一位十进制位数),后跟 '<code>.</code>' (<code>'\u002E'</code>),再后跟表示 <i>a</i> 小数部分的十进制数,再后跟字母 '<code>E</code>' (<code>'\u0045'</code>),最后是用十进制整数形式表示的 <i>n</i>,这与通过方法 <A HREF="Integer.html#toString(int)"><CODE>Integer.toString(int)</CODE></A> 产生的结果相似。 </ul> </ul>必须为 <i>m</i> 或 <i>a</i> 的小数部分显示多少位呢?至少必须有一位数来表示小数部分,除此之外,需要且仅需要必要多的位数来惟一地区别参数值和 <code>double</code> 类型的邻近值。换句话说,假设 <i>x</i> 是用十进制表示法表示的精确算术值,并且是针对有限非零参数 <i>d</i> 通过此方法生成的。那么 <i>d</i> 必须是最接近 <i>x</i> 的 <code>double</code> 值;或者如果两个 <code>double</code> 值都同样接近 <i>x</i>,那么 <i>d</i> 必须是其中之一,并且 <i>d</i> 的最低有效位必须是 <code>0</code>。 <p>使用 <CODE>NumberFormat</CODE> 的子类创建浮点值的本地化字符串表示形式。<P><DD><DL></DL></DD><DD><DL><DT><B>参数:</B><DD><CODE>d</CODE> - 要转换的 <code>double</code> 值。<DT><B>返回:</B><DD>参数的字符串表示形式。</DL></DD></DL><HR><A NAME="toHexString(double)"><!-- --></A><H3>toHexString</H3><PRE>public static <A HREF="String.html" title="java.lang 中的类">String</A> <B>toHexString</B>(double&nbsp;d)</PRE><DL><DD>返回 <code>double</code> 参数的十六进制字符串表示形式。下面提到的所有字符都是 ASCII 字符。 <ul><li>如果参数为 NaN,则结果是字符串 &quot;<code>NaN</code>&quot;。<li>否则,结果是表示参数的符号和大小的字符串。如果符号为负,则结果的第一个字符是 '<code>-</code>' (<code>'\u002D'</code>);如果符号为正,则结果中不显示符号字符。至于大小值 <i>m</i>: <ul> <li>如果 <i>m</i> 为无穷大,则用字符串 <code>&quot;Infinity&quot;</code> 表示它;因此,正无穷大产生的结果就是 <code>&quot;Infinity&quot;</code>,而负无穷大产生的结果是 <code>&quot;-Infinity&quot;</code>。<li>如果 <i>m</i> 为零,则用字符串 <code>&quot;0x0.0p0&quot;</code> 表示它;因此,负零产生的结果就是 <code>&quot;-0x0.0p0&quot;</code>,而正零产生的结果是 <code>&quot;0x0.0p0&quot;</code>。<li>如果 <i>m</i> 是采用正规表示形式的 <code>double</code> 值,则使用子串来表示有效数和指数字段。有效数用字符串 <code>&quot;0x1.&quot;</code> 表示,后跟有效数剩余部分的小写十六进制表示形式,以作为小数部分。除非所有位数为零,否则移除十六进制表示中的尾部零,在所有位数为零的情况下,可以使用一个零表示。其次,指数是用 <code>&quot;p&quot;</code> 表示的,后面跟着无偏指数的十进制字符串,该值与对指数值调用 <A HREF="Integer.html#toString(int)"><CODE>Integer.toString</CODE></A> 所产生的值相同。<li>如果 <i>m</i> 是采用次正规表示形式的 <code>double</code> 值,则用字符 <code>&quot;0x0.&quot;</code> 表示有效数,后跟有效数剩余部分的十六进制表示形式,以作为小数部分。移除十六进制表示形式中的尾部零。然后用 <code>&quot;p-1022&quot;</code> 表示指数。注意,在次正规表示形式有效数中,至少必须有一个非零位数。 </ul>  </ul> <table border><caption><h3>示例</h3></caption><tr><th>浮点值</th><th>十六进制字符串</th> <tr><td><code>1.0</code></td>  <td><code>0x1.0p0</code></td> <tr><td><code>-1.0</code></td> <td><code>-0x1.0p0</code></td> <tr><td><code>2.0</code></td>  <td><code>0x1.0p1</code></td> <tr><td><code>3.0</code></td>  <td><code>0x1.8p1</code></td> <tr><td><code>0.5</code></td>  <td><code>0x1.0p-1</code></td> <tr><td><code>0.25</code></td> <td><code>0x1.0p-2</code></td> <tr><td><code>Double.MAX_VALUE</code></td>     <td><code>0x1.fffffffffffffp1023</code></td> <tr><td><code>Minimum Normal Value</code></td>     <td><code>0x1.0p-1022</code></td> <tr><td><code>Maximum Subnormal Value</code></td>     <td><code>0x0.fffffffffffffp-1022</code></td> <tr><td><code>Double.MIN_VALUE</code></td>     <td><code>0x0.0000000000001p-1022</code></td> </table><P><DD><DL></DL></DD><DD><DL><DT><B>参数:</B><DD><CODE>d</CODE> - 要转换的 <code>double</code> 值。<DT><B>返回:</B><DD>参数的十六进制字符串表示形式。<DT><B>从以下版本开始:</B></DT>  <DD>1.5</DD></DL></DD></DL><HR><A NAME="valueOf(java.lang.String)"><!-- --></A><H3>valueOf</H3><PRE>public static <A HREF="Double.html" title="java.lang 中的类">Double</A> <B>valueOf</B>(<A HREF="String.html" title="java.lang 中的类">String</A>&nbsp;s)                      throws <A HREF="NumberFormatException.html" title="java.lang 中的类">NumberFormatException</A></PRE><DL><DD>返回保存用参数字符串 <code>s</code> 表示的 <code>double</code> 值的 <code>Double</code> 对象。 <p>如果 <code>s</code> 为 <code>null</code>,那么抛出 <code>NullPointerException</code> 异常。<p>忽略 <code>s</code> 中的前导空白字符和尾部空白字符。就像调用 <A HREF="String.html#trim()"><CODE>String.trim()</CODE></A> 方法那样移除空白;换句话说,ASCII 空格和控制字符都要移除。<code>s</code> 的其余部分应按以下词法语法规则的描述构成一个 <i>FloatValue</i>: <blockquote> <dl> <dt><i>FloatValue:</i> <dd><i>Sign<sub>opt</sub></i> <code>NaN</code> <dd><i>Sign<sub>opt</sub></i> <code>Infinity</code> <dd><i>Sign<sub>opt</sub> FloatingPointLiteral</i> <dd><i>Sign<sub>opt</sub> HexFloatingPointLiteral</i> <dd><i>SignedInteger</i> </dl> <p> <dl> <dt><i>HexFloatingPointLiteral</i>: <dd> <i>HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></i> </dl> <p> <dl> <dt><i>HexSignificand:</i> <dd><i>HexNumeral</i> <dd><i>HexNumeral</i> <code>.</code> <dd><code>0x</code> <i>HexDigits<sub>opt</sub></i><code>.</code><i> HexDigits</i> <dd><code>0X</code><i> HexDigits<sub>opt</sub></i><code>.</code> <i>HexDigits</i> </dl> <p> <dl> <dt><i>BinaryExponent:</i> <dd><i>BinaryExponentIndicator SignedInteger</i> </dl> <p> <dl> <dt><i>BinaryExponentIndicator:</i> <dd><code>p</code> <dd><code>P</code> </dl> </blockquote>其中,<i>Sign</i>、<i>FloatingPointLiteral</i>、<i>HexNumeral</i>、<i>HexDigits</i>、<i>SignedInteger</i> 和 <i>FloatTypeSuffix</i> 与《<a  href="../../../../../../../../java.sun.com/docs/books/jls/html/default.htm">Java Language Specification</a>》的词法结构部分中定义的相同。如果 <code>s</code> 的表示形式不是 <i>FloatValue</i>,则抛出 <code>NumberFormatException</code> 异常。否则,可以认为 <code>s</code> 表示的是常碰到的&ldquo;计算机科学记数法&rdquo;中的精确的十进制值,或者是一个精确的十六进制值;在理论上,这个精确的数值被转换一个&ldquo;无限精确的&rdquo;二进制值,然后根据常用的 IEEE 754 浮点算术的&ldquo;舍入成最接近的数&rdquo;规则对 <code>double</code> 类型的值进行舍入计算,其中包括保留零值的符号。最终将返回表示此 <code>double</code> 值的 <code>Double</code> 对象。<p> 使用 <CODE>NumberFormat</CODE> 的子类来解释浮点值的本地化字符串表示形式。<p>注意尾部格式说明符,确定浮点字面值(<code>1.0f</code> 是一个 <code>float</code> 值;<code>1.0d</code> 是一个 <code>double</code> 值)的类型的说明符<em>不会</em> 影响此方法的结果。换句话说,输入字符串的数值被直接转换为目标浮点类型。转换先后分两步,先将字符串转换为 <code>float</code>,然后将 <code>float</code> 转换为 <code>double</code>,这种转换<em>不</em> 同于直接将字符串转换为 <code>double</code>。例如,<code>float</code> 字面值 <code>0.1f</code> 等于 <code>double</code> 值 <code>0.10000000149011612</code>;<code>float</code> 字面值 <code>0.1f</code> 表示与 <code>double</code> 字面值 <code>0.1</code> 不同的数值。(数值 0.1 无法用二进制浮点数字准确表示。)<p>为了避免因为在无效字符串上调用此方法而导致抛出 <code>NumberFormatException</code>,可以使用以下正则表达式作为输入到屏幕的字符串: <code> <pre>        final String Digits     = &quot;(\\p{Digit}+)&quot;;  final String HexDigits  = &quot;(\\p{XDigit}+)&quot;;        // an exponent is 'e' or 'E' followed by an optionally         // .        final String Exp        = &quot;[eE][+-]?&quot;+Digits;        final String fpRegex    =            (&quot;[\\x00-\\x20]*&quot;+        // Optional leading &quot;whitespace&quot;             &quot;[+-]?(&quot; +       // Optional sign character             &quot;NaN|&quot; +         // &quot;NaN&quot; string             &quot;Infinity|&quot; +    // &quot;Infinity&quot; string             // A decimal floating-point string representing a finite positive             // number without a leading sign has at most five basic pieces:             // Digits . Digits ExponentPart FloatTypeSuffix             //              // Since this method allows integer-only strings as input             // in addition to strings of floating-point literals, the             // two sub-patterns below are simplifications of the grammar             // productions from the Java Language Specification, 2nd              // edition, section 3.10.2.             // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt             &quot;(((&quot;+Digits+&quot;(\\.)?(&quot;+Digits+&quot;?)(&quot;+Exp+&quot;)?)|&quot;+             // . Digits ExponentPart_opt FloatTypeSuffix_opt             &quot;(\\.(&quot;+Digits+&quot;)(&quot;+Exp+&quot;)?)|&quot;+       // Hexadecimal strings       &quot;((&quot; +        // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt        &quot;(0[xX]&quot; + HexDigits + &quot;(\\.)?)|&quot; +        // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt        &quot;(0[xX]&quot; + HexDigits + &quot;?(\\.)&quot; + HexDigits + &quot;)&quot; +        &quot;)[pP][+-]?&quot; + Digits + &quot;))&quot; +             &quot;[fFdD]?))&quot; +             &quot;[\\x00-\\x20]*&quot;);// Optional trailing &quot;whitespace&quot;              if (Pattern.matches(fpRegex, myString))            Double.valueOf(myString); // Will not throw NumberFormatException        else {            // Perform suitable alternative action        } </pre> </code><P>

⌨️ 快捷键说明

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