Aracılığıyla paylaş


Double.ValueOf Method

Definition

Overloads

ValueOf(Double)

Returns a Double instance representing the specified double value.

ValueOf(String)

Returns a Double object holding the double value represented by the argument string s.

ValueOf(Double)

Returns a Double instance representing the specified double value.

[Android.Runtime.Register("valueOf", "(D)Ljava/lang/Double;", "")]
public static Java.Lang.Double ValueOf (double d);
[<Android.Runtime.Register("valueOf", "(D)Ljava/lang/Double;", "")>]
static member ValueOf : double -> Java.Lang.Double

Parameters

d
Double

a double value.

Returns

a Double instance representing d.

Attributes

Remarks

Returns a Double instance representing the specified double value. If a new Double instance is not required, this method should generally be used in preference to the constructor #Double(double), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

Added in 1.5.

Java documentation for java.lang.Double.valueOf(double).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

ValueOf(String)

Returns a Double object holding the double value represented by the argument string s.

[Android.Runtime.Register("valueOf", "(Ljava/lang/String;)Ljava/lang/Double;", "")]
public static Java.Lang.Double ValueOf (string s);
[<Android.Runtime.Register("valueOf", "(Ljava/lang/String;)Ljava/lang/Double;", "")>]
static member ValueOf : string -> Java.Lang.Double

Parameters

s
String

the string to be parsed.

Returns

a Double object holding the value represented by the String argument.

Attributes

Exceptions

if string cannot be parsed as a double value.

Remarks

Returns a Double object holding the double value represented by the argument string s.

If s is null, then a NullPointerException is thrown.

Leading and trailing whitespace characters in s are ignored. Whitespace is removed as if by the String#trim method; that is, both ASCII space and control characters are removed. The rest of s should constitute a FloatValue as described by the lexical syntax rules:

<blockquote> <dl> <dt>FloatValue:<dd>Sign<sub>opt</sub>NaN<dd>Sign<sub>opt</sub>Infinity<dd>Sign<sub>opt</sub> FloatingPointLiteral<dd>Sign<sub>opt</sub> HexFloatingPointLiteral<dd>SignedInteger</dl>

<dl> <dt>HexFloatingPointLiteral: <dd> HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></dl>

<dl> <dt>HexSignificand:<dd>HexNumeral<dd>HexNumeral.<dd>0xHexDigits<sub>opt</sub> . HexDigits<dd>0X HexDigits<sub>opt</sub> .HexDigits</dl>

<dl> <dt>BinaryExponent:<dd>BinaryExponentIndicator SignedInteger</dl>

<dl> <dt>BinaryExponentIndicator:<dd>p<dd>P</dl>

</blockquote>

where Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger and FloatTypeSuffix are as defined in the lexical structure sections of <cite>The Java Language Specification</cite>, except that underscores are not accepted between digits. If s does not have the form of a FloatValue, then a NumberFormatException is thrown. Otherwise, s is regarded as representing an exact decimal value in the usual "computerized scientific notation" or as an exact hexadecimal value; this exact numerical value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type double by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value.

Note that the round-to-nearest rule also implies overflow and underflow behaviour; if the exact value of s is large enough in magnitude (greater than or equal to (#MAX_VALUE + Math#ulp(double) ulp(MAX_VALUE)/2), rounding to double will result in an infinity and if the exact value of s is small enough in magnitude (less than or equal to #MIN_VALUE/2), rounding to float will result in a zero.

Finally, after rounding a Double object representing this double value is returned.

To interpret localized string representations of a floating-point value, use subclasses of java.text.NumberFormat.

Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do <em>not</em> influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. The two-step sequence of conversions, string to float followed by float to double, is <em>not</em> equivalent to converting a string directly to double. For example, the float literal 0.1f is equal to the double value 0.10000000149011612; the float literal 0.1f represents a different numerical value than the double literal 0.1. (The numerical value 0.1 cannot be exactly represented in a binary floating-point number.)

To avoid calling this method on an invalid string and having a NumberFormatException be thrown, the regular expression below can be used to screen the input string:

{@code
             final String Digits     = "(\\p{Digit}+)";
             final String HexDigits  = "(\\p{XDigit}+)";
             // an exponent is 'e' or 'E' followed by an optionally
             // signed decimal integer.
             final String Exp        = "[eE][+-]?"+Digits;
             final String fpRegex    =
                 ("[\\x00-\\x20]*"+  // Optional leading "whitespace"
                  "[+-]?(" + // Optional sign character
                  "NaN|" +           // "NaN" string
                  "Infinity|" +      // "Infinity" 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 section 3.10.2 of
                  // The Java Language Specification.

                  // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
                  "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+

                  // . Digits ExponentPart_opt FloatTypeSuffix_opt
                  "(\\.("+Digits+")("+Exp+")?)|"+

                  // Hexadecimal strings
                  "((" +
                   // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
                   "(0[xX]" + HexDigits + "(\\.)?)|" +

                   // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
                   "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +

                   ")[pP][+-]?" + Digits + "))" +
                  "[fFdD]?))" +
                  "[\\x00-\\x20]*");// Optional trailing "whitespace"

             if (Pattern.matches(fpRegex, myString))
                 Double.valueOf(myString); // Will not throw NumberFormatException
             else {
                 // Perform suitable alternative action
             }
            }

Java documentation for java.lang.Double.valueOf(java.lang.String).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

See also

Applies to