src/share/classes/java/lang/Double.java

Print this page


   1 /*
   2  * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 123     public static final int SIZE = 64;
 124 
 125     /**
 126      * The {@code Class} instance representing the primitive type
 127      * {@code double}.
 128      *
 129      * @since JDK1.1
 130      */
 131     @SuppressWarnings("unchecked")
 132     public static final Class<Double>   TYPE = (Class<Double>) Class.getPrimitiveClass("double");
 133 
 134     /**
 135      * Returns a string representation of the {@code double}
 136      * argument. All characters mentioned below are ASCII characters.
 137      * <ul>
 138      * <li>If the argument is NaN, the result is the string
 139      *     "{@code NaN}".
 140      * <li>Otherwise, the result is a string that represents the sign and
 141      * magnitude (absolute value) of the argument. If the sign is negative,
 142      * the first character of the result is '{@code -}'
 143      * (<code>'&#92;u002D'</code>); if the sign is positive, no sign character
 144      * appears in the result. As for the magnitude <i>m</i>:
 145      * <ul>
 146      * <li>If <i>m</i> is infinity, it is represented by the characters
 147      * {@code "Infinity"}; thus, positive infinity produces the result
 148      * {@code "Infinity"} and negative infinity produces the result
 149      * {@code "-Infinity"}.
 150      *
 151      * <li>If <i>m</i> is zero, it is represented by the characters
 152      * {@code "0.0"}; thus, negative zero produces the result
 153      * {@code "-0.0"} and positive zero produces the result
 154      * {@code "0.0"}.
 155      *
 156      * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
 157      * than 10<sup>7</sup>, then it is represented as the integer part of
 158      * <i>m</i>, in decimal form with no leading zeroes, followed by
 159      * '{@code .}' (<code>'&#92;u002E'</code>), followed by one or
 160      * more decimal digits representing the fractional part of <i>m</i>.
 161      *
 162      * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
 163      * equal to 10<sup>7</sup>, then it is represented in so-called
 164      * "computerized scientific notation." Let <i>n</i> be the unique
 165      * integer such that 10<sup><i>n</i></sup> &le; <i>m</i> {@literal <}
 166      * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
 167      * mathematically exact quotient of <i>m</i> and
 168      * 10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10. The
 169      * magnitude is then represented as the integer part of <i>a</i>,
 170      * as a single decimal digit, followed by '{@code .}'
 171      * (<code>'&#92;u002E'</code>), followed by decimal digits
 172      * representing the fractional part of <i>a</i>, followed by the
 173      * letter '{@code E}' (<code>'&#92;u0045'</code>), followed
 174      * by a representation of <i>n</i> as a decimal integer, as
 175      * produced by the method {@link Integer#toString(int)}.
 176      * </ul>
 177      * </ul>
 178      * How many digits must be printed for the fractional part of
 179      * <i>m</i> or <i>a</i>? There must be at least one digit to represent
 180      * the fractional part, and beyond that as many, but only as many, more
 181      * digits as are needed to uniquely distinguish the argument value from
 182      * adjacent values of type {@code double}. That is, suppose that
 183      * <i>x</i> is the exact mathematical value represented by the decimal
 184      * representation produced by this method for a finite nonzero argument
 185      * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
 186      * to <i>x</i>; or if two {@code double} values are equally close
 187      * to <i>x</i>, then <i>d</i> must be one of them and the least
 188      * significant bit of the significand of <i>d</i> must be {@code 0}.
 189      *
 190      * <p>To create localized string representations of a floating-point
 191      * value, use subclasses of {@link java.text.NumberFormat}.
 192      *
 193      * @param   d   the {@code double} to be converted.
 194      * @return a string representation of the argument.
 195      */
 196     public static String toString(double d) {
 197         return new FloatingDecimal(d).toJavaFormatString();
 198     }
 199 
 200     /**
 201      * Returns a hexadecimal string representation of the
 202      * {@code double} argument. All characters mentioned below
 203      * are ASCII characters.
 204      *
 205      * <ul>
 206      * <li>If the argument is NaN, the result is the string
 207      *     "{@code NaN}".
 208      * <li>Otherwise, the result is a string that represents the sign
 209      * and magnitude of the argument. If the sign is negative, the
 210      * first character of the result is '{@code -}'
 211      * (<code>'&#92;u002D'</code>); if the sign is positive, no sign
 212      * character appears in the result. As for the magnitude <i>m</i>:
 213      *
 214      * <ul>
 215      * <li>If <i>m</i> is infinity, it is represented by the string
 216      * {@code "Infinity"}; thus, positive infinity produces the
 217      * result {@code "Infinity"} and negative infinity produces
 218      * the result {@code "-Infinity"}.
 219      *
 220      * <li>If <i>m</i> is zero, it is represented by the string
 221      * {@code "0x0.0p0"}; thus, negative zero produces the result
 222      * {@code "-0x0.0p0"} and positive zero produces the result
 223      * {@code "0x0.0p0"}.
 224      *
 225      * <li>If <i>m</i> is a {@code double} value with a
 226      * normalized representation, substrings are used to represent the
 227      * significand and exponent fields.  The significand is
 228      * represented by the characters {@code "0x1."}
 229      * followed by a lowercase hexadecimal representation of the rest
 230      * of the significand as a fraction.  Trailing zeros in the
 231      * hexadecimal representation are removed unless all the digits


   1 /*
   2  * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 123     public static final int SIZE = 64;
 124 
 125     /**
 126      * The {@code Class} instance representing the primitive type
 127      * {@code double}.
 128      *
 129      * @since JDK1.1
 130      */
 131     @SuppressWarnings("unchecked")
 132     public static final Class<Double>   TYPE = (Class<Double>) Class.getPrimitiveClass("double");
 133 
 134     /**
 135      * Returns a string representation of the {@code double}
 136      * argument. All characters mentioned below are ASCII characters.
 137      * <ul>
 138      * <li>If the argument is NaN, the result is the string
 139      *     "{@code NaN}".
 140      * <li>Otherwise, the result is a string that represents the sign and
 141      * magnitude (absolute value) of the argument. If the sign is negative,
 142      * the first character of the result is '{@code -}'
 143      * ({@code '\u005Cu002D'}); if the sign is positive, no sign character
 144      * appears in the result. As for the magnitude <i>m</i>:
 145      * <ul>
 146      * <li>If <i>m</i> is infinity, it is represented by the characters
 147      * {@code "Infinity"}; thus, positive infinity produces the result
 148      * {@code "Infinity"} and negative infinity produces the result
 149      * {@code "-Infinity"}.
 150      *
 151      * <li>If <i>m</i> is zero, it is represented by the characters
 152      * {@code "0.0"}; thus, negative zero produces the result
 153      * {@code "-0.0"} and positive zero produces the result
 154      * {@code "0.0"}.
 155      *
 156      * <li>If <i>m</i> is greater than or equal to 10<sup>-3</sup> but less
 157      * than 10<sup>7</sup>, then it is represented as the integer part of
 158      * <i>m</i>, in decimal form with no leading zeroes, followed by
 159      * '{@code .}' ({@code '\u005Cu002E'}), followed by one or
 160      * more decimal digits representing the fractional part of <i>m</i>.
 161      *
 162      * <li>If <i>m</i> is less than 10<sup>-3</sup> or greater than or
 163      * equal to 10<sup>7</sup>, then it is represented in so-called
 164      * "computerized scientific notation." Let <i>n</i> be the unique
 165      * integer such that 10<sup><i>n</i></sup> &le; <i>m</i> {@literal <}
 166      * 10<sup><i>n</i>+1</sup>; then let <i>a</i> be the
 167      * mathematically exact quotient of <i>m</i> and
 168      * 10<sup><i>n</i></sup> so that 1 &le; <i>a</i> {@literal <} 10. The
 169      * magnitude is then represented as the integer part of <i>a</i>,
 170      * as a single decimal digit, followed by '{@code .}'
 171      * ({@code '\u005Cu002E'}), followed by decimal digits
 172      * representing the fractional part of <i>a</i>, followed by the
 173      * letter '{@code E}' ({@code '\u005Cu0045'}), followed
 174      * by a representation of <i>n</i> as a decimal integer, as
 175      * produced by the method {@link Integer#toString(int)}.
 176      * </ul>
 177      * </ul>
 178      * How many digits must be printed for the fractional part of
 179      * <i>m</i> or <i>a</i>? There must be at least one digit to represent
 180      * the fractional part, and beyond that as many, but only as many, more
 181      * digits as are needed to uniquely distinguish the argument value from
 182      * adjacent values of type {@code double}. That is, suppose that
 183      * <i>x</i> is the exact mathematical value represented by the decimal
 184      * representation produced by this method for a finite nonzero argument
 185      * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
 186      * to <i>x</i>; or if two {@code double} values are equally close
 187      * to <i>x</i>, then <i>d</i> must be one of them and the least
 188      * significant bit of the significand of <i>d</i> must be {@code 0}.
 189      *
 190      * <p>To create localized string representations of a floating-point
 191      * value, use subclasses of {@link java.text.NumberFormat}.
 192      *
 193      * @param   d   the {@code double} to be converted.
 194      * @return a string representation of the argument.
 195      */
 196     public static String toString(double d) {
 197         return new FloatingDecimal(d).toJavaFormatString();
 198     }
 199 
 200     /**
 201      * Returns a hexadecimal string representation of the
 202      * {@code double} argument. All characters mentioned below
 203      * are ASCII characters.
 204      *
 205      * <ul>
 206      * <li>If the argument is NaN, the result is the string
 207      *     "{@code NaN}".
 208      * <li>Otherwise, the result is a string that represents the sign
 209      * and magnitude of the argument. If the sign is negative, the
 210      * first character of the result is '{@code -}'
 211      * ({@code '\u005Cu002D'}); if the sign is positive, no sign
 212      * character appears in the result. As for the magnitude <i>m</i>:
 213      *
 214      * <ul>
 215      * <li>If <i>m</i> is infinity, it is represented by the string
 216      * {@code "Infinity"}; thus, positive infinity produces the
 217      * result {@code "Infinity"} and negative infinity produces
 218      * the result {@code "-Infinity"}.
 219      *
 220      * <li>If <i>m</i> is zero, it is represented by the string
 221      * {@code "0x0.0p0"}; thus, negative zero produces the result
 222      * {@code "-0x0.0p0"} and positive zero produces the result
 223      * {@code "0x0.0p0"}.
 224      *
 225      * <li>If <i>m</i> is a {@code double} value with a
 226      * normalized representation, substrings are used to represent the
 227      * significand and exponent fields.  The significand is
 228      * represented by the characters {@code "0x1."}
 229      * followed by a lowercase hexadecimal representation of the rest
 230      * of the significand as a fraction.  Trailing zeros in the
 231      * hexadecimal representation are removed unless all the digits