Updated: May 2009
Converts the string representation of a number in a specified culture-specific format 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, _
provider As IFormatProvider _
) As Double
Dim s As String
Dim provider As IFormatProvider
Dim returnValue As Double
returnValue = Double.Parse(s, provider)
public static double Parse(
string s,
IFormatProvider provider
)
public:
static double Parse(
String^ s,
IFormatProvider^ provider
)
public static function Parse(
s : String,
provider : IFormatProvider
) : 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.
This overload of the Parse(String, IFormatProvider) method is typically used to convert text that can be formatted in a variety of ways to a Double value. For example, it can be used to convert the text entered by a user into an HTML text box to a numeric value.
The s parameter is interpreted using a combination of the NumberStyles..::.Float and NumberStyles..::.AllowThousands flags. The s parameter can contain NumberFormatInfo..::.PositiveInfinitySymbol, NumberFormatInfo..::.NegativeInfinitySymbol, NumberFormatInfo..::.NaNSymbol for the culture specified by provider, or it can contain a string of the form:
[ws][sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws]
Optional items are framed in square brackets ([ and ]). Items containing the term "digits" consist of a series of numeric characters ranging from 0 to 9.
Element | Description |
|---|
ws
| A series of white space characters. |
sign
| A negative sign or positive sign symbol. |
integral-digits
| A series of digits specifying the integral part of the number. Runs of integral-digits can be partitioned by a group-separator symbol. (For example, in some cultures a comma (,) separates groups of thousands.) Integral-digits can be absent if there are fractional-digits. |
.
| A culture-specific decimal point symbol. |
fractional-digits
| A series of digits specifying the fractional part of the number. |
E
| An uppercase or lowercase character 'e', indicating exponential (scientific) notation. |
exponential-digits
| A series of digits specifying an exponent. |
For more information about numeric formats, see the Formatting Overview topic.
The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that supplies culture-specific information used in interpreting the format of s. Typically, it is a NumberFormatInfo object or a CultureInfo object. If provider is nullNothingnullptra null reference (Nothing in Visual Basic) or a NumberFormatInfo cannot be obtained, the formatting information for the current system culture is used.
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 is the button click event handler of a Web form. It uses the array returned by the HttpRequest..::.UserLanguages property to determine the user's locale. It then instantiates a CultureInfo object that corresponds to that locale. The NumberFormatInfo object that belongs to that CultureInfo object is then passed to the Parse(String, IFormatProvider) method to convert the user's input to a Double value.
Protected Sub OkToDouble_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToDouble.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Double
' Return if string is empty
If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub
' Get locale of web request to determine possible format of number
If Request.UserLanguages.Length = 0 Then Exit Sub
locale = Request.UserLanguages(0)
If String.IsNullOrEmpty(locale) Then Exit Sub
' Instantiate CultureInfo object for the user's locale
culture = New CultureInfo(locale)
' Convert user input from a string to a number
Try
number = Double.Parse(Me.inputNumber.Text, culture.NumberFormat)
Catch ex As FormatException
Exit Sub
Catch ex As Exception
Exit Sub
End Try
' Output number to label on web form
Me.outputNumber.Text = "Number is " & number.ToString()
End Sub
protected void OkToDouble_Click(object sender, EventArgs e)
{
string locale;
double number;
CultureInfo culture;
// Return if string is empty
if (String.IsNullOrEmpty(this.inputNumber.Text))
return;
// Get locale of web request to determine possible format of number
if (Request.UserLanguages.Length == 0)
return;
locale = Request.UserLanguages[0];
if (String.IsNullOrEmpty(locale))
return;
// Instantiate CultureInfo object for the user's locale
culture = new CultureInfo(locale);
// Convert user input from a string to a number
try
{
number = Double.Parse(this.inputNumber.Text, culture.NumberFormat);
}
catch (FormatException)
{
return;
}
catch (Exception)
{
return;
}
// Output number to label on web form
this.outputNumber.Text = "Number is " + number.ToString();
}
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.
|