Updated: May 2009
Converts the string representation of a number in a specified style to its double-precision floating-point number equivalent.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Function Parse ( _
s As String, _
style As NumberStyles _
) As Double
Dim s As String
Dim style As NumberStyles
Dim returnValue As Double
returnValue = Double.Parse(s, style)
public static double Parse(
string s,
NumberStyles style
)
public:
static double Parse(
String^ s,
NumberStyles style
)
public static function Parse(
s : String,
style : NumberStyles
) : double
Return Value
Type:
System..::.DoubleA double-precision floating-point number equivalent to the numeric value or symbol specified in s. Because of differences in precision, the return value may not be exactly equal to s, and for values of s that are less than Epsilon, the return value may also differ depending on processor architecture. For more information, see the Remarks section of Double.
The style parameter defines the style elements (such as white space, thousands separators, and currency symbols) that are allowed in the s parameter for the parse operation to succeed. It must be a combination of bit flags from the NumberStyles enumeration. The following NumberStyles members are not supported:
The s parameter can contain the current culture's NumberFormatInfo..::.PositiveInfinitySymbol, NumberFormatInfo..::.NegativeInfinitySymbol, or NumberFormatInfo..::.NaNSymbol. Depending on the value of style, it can also take the form:
[ws][$][sign][integral-digits[,]]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws]
Elements in square brackets ([ and ]) are optional. The following table describes each element.
A string with digits only (which corresponds to the NumberStyles..::.None style) always parses successfully. The remaining System.Globalization..::.NumberStyles members control elements that may be but are not required to be present in the input string. The following table indicates how individual NumberStyles flags affect the elements that may be present in s.
NumberStyles value | Elements permitted in s in addition to digits |
|---|
None
| The digits element only. |
AllowDecimalPoint
| The . and fractional-digits elements. |
AllowExponent
| The s parameter can also use exponential notation. This flag by itself supports values in the form digitsEdigits; additional flags are needed to successfully parse strings with such elements as positive or negative signs and decimal point symbols. |
AllowLeadingWhite
| The ws element at the beginning of s. |
AllowTrailingWhite
| The ws element at the end of s. |
AllowLeadingSign
| The sign element at the beginning of s. |
AllowTrailingSign
| The sign element at the end of s. |
AllowParentheses
| The sign element in the form of parentheses enclosing the numeric value. |
AllowThousands
| The , element. |
AllowCurrencySymbol
| The $ element. |
Currency
| All. The s parameter cannot represent a hexadecimal number or a number in exponential notation. |
Float
| The ws element at the beginning or end of s, sign at the beginning of s, and the . symbol. The s parameter can also use exponential notation. |
Number
| The ws, sign, , and . elements. |
Any
| All styles, except s cannot represent a hexadecimal number. |
The s parameter is parsed using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. For more information, see CurrentInfo.
Ordinarily, if you pass the Double..::.Parse method a string that is created by calling the Double..::.ToString method, the original Double value is returned. However, because of a loss of precision, the values may not be equal. In addition, attempting to parse the string representation of either MinValue or MaxValue throws an OverflowException, as the following example illustrates.
Dim value As String
value = Double.MinValue.ToString()
Try
Console.WriteLine(Double.Parse(value))
Catch e As OverflowException
Console.WriteLine("{0} is outside the range of the Double type.", _
value)
End Try
value = Double.MaxValue.ToString()
Try
Console.WriteLine(Double.Parse(value))
Catch e As OverflowException
Console.WriteLine("{0} is outside the range of the Double type.", _
value)
End Try
' The example displays the following output:
' -1.79769313486232E+308 is outside the range of the Double type.
' 1.79769313486232E+308 is outside the range of the Double type.
string value;
value = Double.MinValue.ToString();
try {
Console.WriteLine(Double.Parse(value));
}
catch (OverflowException) {
Console.WriteLine("{0} is outside the range of the Double type.",
value);
}
value = Double.MaxValue.ToString();
try {
Console.WriteLine(Double.Parse(value));
}
catch (OverflowException) {
Console.WriteLine("{0} is outside the range of the Double type.",
value);
}
// The example displays the following output:
// -1.79769313486232E+308 is outside the range of the Double type.
// 1.79769313486232E+308 is outside the range of the Double type.
If a separator is encountered in the s parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.
The following example uses the Double..::.Parse(String, NumberStyles) method to parse the string representations of Double values using the en-US culture.
Public Sub Main()
' Set current thread culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim value As String
Dim styles As NumberStyles
' Parse a string in exponential notation with only the AllowExponent flag.
value = "-1.063E-02"
styles = NumberStyles.AllowExponent
ShowNumericValue(value, styles)
' Parse a string in exponential notation
' with the AllowExponent and Number flags.
styles = NumberStyles.AllowExponent Or NumberStyles.Number
ShowNumericValue(value, styles)
' Parse a currency value with leading and trailing white space, and
' white space after the U.S. currency symbol.
value = " $ 6,164.3299 "
styles = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
ShowNumericValue(value, styles)
' Parse negative value with thousands separator and decimal.
value = "(4,320.64)"
styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
Or NumberStyles.Float
ShowNumericValue(value, styles)
styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
Or NumberStyles.Float Or NumberStyles.AllowThousands
ShowNumericValue(value, styles)
End Sub
Private Sub ShowNumericValue(value As String, styles As NumberStyles)
Dim number As Double
Try
number = Double.Parse(value, styles)
Console.WriteLine("Converted '{0}' using {1} to {2}.", _
value, styles.ToString(), number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}' with styles {1}.", _
value, styles.ToString())
End Try
Console.WriteLine()
End Sub
' The example displays the following output to the console:
' Unable to parse '-1.063E-02' with styles AllowExponent.
'
' Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063.
'
' Converted ' $ 6,164.3299 ' using Number, AllowCurrencySymbol to 6164.3299.
'
' Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float.
'
' Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.
public static void Main()
{
// Set current thread culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
string value;
NumberStyles styles;
// Parse a string in exponential notation with only the AllowExponent flag.
value = "-1.063E-02";
styles = NumberStyles.AllowExponent;
ShowNumericValue(value, styles);
// Parse a string in exponential notation
// with the AllowExponent and Number flags.
styles = NumberStyles.AllowExponent | NumberStyles.Number;
ShowNumericValue(value, styles);
// Parse a currency value with leading and trailing white space, and
// white space after the U.S. currency symbol.
value = " $ 6,164.3299 ";
styles = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
ShowNumericValue(value, styles);
// Parse negative value with thousands separator and decimal.
value = "(4,320.64)";
styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
NumberStyles.Float;
ShowNumericValue(value, styles);
styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
NumberStyles.Float | NumberStyles.AllowThousands;
ShowNumericValue(value, styles);
}
private static void ShowNumericValue(string value, NumberStyles styles)
{
double number;
try
{
number = Double.Parse(value, styles);
Console.WriteLine("Converted '{0}' using {1} to {2}.",
value, styles.ToString(), number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}' with styles {1}.",
value, styles.ToString());
}
Console.WriteLine();
}
// The example displays the following output to the console:
// Unable to parse '-1.063E-02' with styles AllowExponent.
//
// Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063.
//
// Converted ' $ 6,164.3299 ' using Number, AllowCurrencySymbol to 6164.3299.
//
// Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float.
//
// Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference
Other Resources
Date | History | Reason |
|---|
May 2009
| Expanded the Return Value section. |
Content bug fix.
|
March 2009
| Expanded the Remarks section. |
Content bug fix.
|