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

Print this page




 184      * </ul>
 185      * How many digits must be printed for the fractional part of
 186      * <i>m</i> or <i>a</i>? There must be at least one digit to represent
 187      * the fractional part, and beyond that as many, but only as many, more
 188      * digits as are needed to uniquely distinguish the argument value from
 189      * adjacent values of type {@code double}. That is, suppose that
 190      * <i>x</i> is the exact mathematical value represented by the decimal
 191      * representation produced by this method for a finite nonzero argument
 192      * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
 193      * to <i>x</i>; or if two {@code double} values are equally close
 194      * to <i>x</i>, then <i>d</i> must be one of them and the least
 195      * significant bit of the significand of <i>d</i> must be {@code 0}.
 196      *
 197      * <p>To create localized string representations of a floating-point
 198      * value, use subclasses of {@link java.text.NumberFormat}.
 199      *
 200      * @param   d   the {@code double} to be converted.
 201      * @return a string representation of the argument.
 202      */
 203     public static String toString(double d) {
 204         return new FloatingDecimal(d).toJavaFormatString();
 205     }
 206 
 207     /**
 208      * Returns a hexadecimal string representation of the
 209      * {@code double} argument. All characters mentioned below
 210      * are ASCII characters.
 211      *
 212      * <ul>
 213      * <li>If the argument is NaN, the result is the string
 214      *     "{@code NaN}".
 215      * <li>Otherwise, the result is a string that represents the sign
 216      * and magnitude of the argument. If the sign is negative, the
 217      * first character of the result is '{@code -}'
 218      * ({@code '\u005Cu002D'}); if the sign is positive, no sign
 219      * character appears in the result. As for the magnitude <i>m</i>:
 220      *
 221      * <ul>
 222      * <li>If <i>m</i> is infinity, it is represented by the string
 223      * {@code "Infinity"}; thus, positive infinity produces the
 224      * result {@code "Infinity"} and negative infinity produces


 492      *
 493      *        ")[pP][+-]?" + Digits + "))" +
 494      *       "[fFdD]?))" +
 495      *       "[\\x00-\\x20]*");// Optional trailing "whitespace"
 496      *
 497      *  if (Pattern.matches(fpRegex, myString))
 498      *      Double.valueOf(myString); // Will not throw NumberFormatException
 499      *  else {
 500      *      // Perform suitable alternative action
 501      *  }
 502      * </pre>
 503      * </code>
 504      *
 505      * @param      s   the string to be parsed.
 506      * @return     a {@code Double} object holding the value
 507      *             represented by the {@code String} argument.
 508      * @throws     NumberFormatException  if the string does not contain a
 509      *             parsable number.
 510      */
 511     public static Double valueOf(String s) throws NumberFormatException {
 512         return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue());
 513     }
 514 
 515     /**
 516      * Returns a {@code Double} instance representing the specified
 517      * {@code double} value.
 518      * If a new {@code Double} instance is not required, this method
 519      * should generally be used in preference to the constructor
 520      * {@link #Double(double)}, as this method is likely to yield
 521      * significantly better space and time performance by caching
 522      * frequently requested values.
 523      *
 524      * @param  d a double value.
 525      * @return a {@code Double} instance representing {@code d}.
 526      * @since  1.5
 527      */
 528     public static Double valueOf(double d) {
 529         return new Double(d);
 530     }
 531 
 532     /**
 533      * Returns a new {@code double} initialized to the value
 534      * represented by the specified {@code String}, as performed
 535      * by the {@code valueOf} method of class
 536      * {@code Double}.
 537      *
 538      * @param  s   the string to be parsed.
 539      * @return the {@code double} value represented by the string
 540      *         argument.
 541      * @throws NullPointerException  if the string is null
 542      * @throws NumberFormatException if the string does not contain
 543      *         a parsable {@code double}.
 544      * @see    java.lang.Double#valueOf(String)
 545      * @since 1.2
 546      */
 547     public static double parseDouble(String s) throws NumberFormatException {
 548         return FloatingDecimal.readJavaFormatString(s).doubleValue();
 549     }
 550 
 551     /**
 552      * Returns {@code true} if the specified number is a
 553      * Not-a-Number (NaN) value, {@code false} otherwise.
 554      *
 555      * @param   v   the value to be tested.
 556      * @return  {@code true} if the value of the argument is NaN;
 557      *          {@code false} otherwise.
 558      */
 559     public static boolean isNaN(double v) {
 560         return (v != v);
 561     }
 562 
 563     /**
 564      * Returns {@code true} if the specified number is infinitely
 565      * large in magnitude, {@code false} otherwise.
 566      *
 567      * @param   v   the value to be tested.
 568      * @return  {@code true} if the value of the argument is positive




 184      * </ul>
 185      * How many digits must be printed for the fractional part of
 186      * <i>m</i> or <i>a</i>? There must be at least one digit to represent
 187      * the fractional part, and beyond that as many, but only as many, more
 188      * digits as are needed to uniquely distinguish the argument value from
 189      * adjacent values of type {@code double}. That is, suppose that
 190      * <i>x</i> is the exact mathematical value represented by the decimal
 191      * representation produced by this method for a finite nonzero argument
 192      * <i>d</i>. Then <i>d</i> must be the {@code double} value nearest
 193      * to <i>x</i>; or if two {@code double} values are equally close
 194      * to <i>x</i>, then <i>d</i> must be one of them and the least
 195      * significant bit of the significand of <i>d</i> must be {@code 0}.
 196      *
 197      * <p>To create localized string representations of a floating-point
 198      * value, use subclasses of {@link java.text.NumberFormat}.
 199      *
 200      * @param   d   the {@code double} to be converted.
 201      * @return a string representation of the argument.
 202      */
 203     public static String toString(double d) {
 204         return FloatingDecimal.toJavaFormatString(d);
 205     }
 206 
 207     /**
 208      * Returns a hexadecimal string representation of the
 209      * {@code double} argument. All characters mentioned below
 210      * are ASCII characters.
 211      *
 212      * <ul>
 213      * <li>If the argument is NaN, the result is the string
 214      *     "{@code NaN}".
 215      * <li>Otherwise, the result is a string that represents the sign
 216      * and magnitude of the argument. If the sign is negative, the
 217      * first character of the result is '{@code -}'
 218      * ({@code '\u005Cu002D'}); if the sign is positive, no sign
 219      * character appears in the result. As for the magnitude <i>m</i>:
 220      *
 221      * <ul>
 222      * <li>If <i>m</i> is infinity, it is represented by the string
 223      * {@code "Infinity"}; thus, positive infinity produces the
 224      * result {@code "Infinity"} and negative infinity produces


 492      *
 493      *        ")[pP][+-]?" + Digits + "))" +
 494      *       "[fFdD]?))" +
 495      *       "[\\x00-\\x20]*");// Optional trailing "whitespace"
 496      *
 497      *  if (Pattern.matches(fpRegex, myString))
 498      *      Double.valueOf(myString); // Will not throw NumberFormatException
 499      *  else {
 500      *      // Perform suitable alternative action
 501      *  }
 502      * </pre>
 503      * </code>
 504      *
 505      * @param      s   the string to be parsed.
 506      * @return     a {@code Double} object holding the value
 507      *             represented by the {@code String} argument.
 508      * @throws     NumberFormatException  if the string does not contain a
 509      *             parsable number.
 510      */
 511     public static Double valueOf(String s) throws NumberFormatException {
 512         return new Double(parseDouble(s));
 513     }
 514 
 515     /**
 516      * Returns a {@code Double} instance representing the specified
 517      * {@code double} value.
 518      * If a new {@code Double} instance is not required, this method
 519      * should generally be used in preference to the constructor
 520      * {@link #Double(double)}, as this method is likely to yield
 521      * significantly better space and time performance by caching
 522      * frequently requested values.
 523      *
 524      * @param  d a double value.
 525      * @return a {@code Double} instance representing {@code d}.
 526      * @since  1.5
 527      */
 528     public static Double valueOf(double d) {
 529         return new Double(d);
 530     }
 531 
 532     /**
 533      * Returns a new {@code double} initialized to the value
 534      * represented by the specified {@code String}, as performed
 535      * by the {@code valueOf} method of class
 536      * {@code Double}.
 537      *
 538      * @param  s   the string to be parsed.
 539      * @return the {@code double} value represented by the string
 540      *         argument.
 541      * @throws NullPointerException  if the string is null
 542      * @throws NumberFormatException if the string does not contain
 543      *         a parsable {@code double}.
 544      * @see    java.lang.Double#valueOf(String)
 545      * @since 1.2
 546      */
 547     public static double parseDouble(String s) throws NumberFormatException {
 548         return FloatingDecimal.parseDouble(s);
 549     }
 550 
 551     /**
 552      * Returns {@code true} if the specified number is a
 553      * Not-a-Number (NaN) value, {@code false} otherwise.
 554      *
 555      * @param   v   the value to be tested.
 556      * @return  {@code true} if the value of the argument is NaN;
 557      *          {@code false} otherwise.
 558      */
 559     public static boolean isNaN(double v) {
 560         return (v != v);
 561     }
 562 
 563     /**
 564      * Returns {@code true} if the specified number is infinitely
 565      * large in magnitude, {@code false} otherwise.
 566      *
 567      * @param   v   the value to be tested.
 568      * @return  {@code true} if the value of the argument is positive