Converts the string representation of a number in a specified culture-specific format to its single-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 Single
Dim s As String
Dim provider As IFormatProvider
Dim returnValue As Single
returnValue = Single.Parse(s, provider)
public static float Parse(
string s,
IFormatProvider provider
)
public:
static float Parse(
String^ s,
IFormatProvider^ provider
)
public static function Parse(
s : String,
provider : IFormatProvider
) : float
Return Value
Type:
System..::.SingleA single-precision floating-point number equivalent to the numeric value or symbol specified in s.
This overload is typically used to convert text that can be formatted in a variety of ways to a Single 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 can contain PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or 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.
- 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.
Some examples of s are "100", "-123,456,789", "123.45e+6", "+500", "5e2", "3.1416", "600.", "-.123", and "-Infinity".
This overload of Parse uses the NumberStyles..::.Float and NumberStyles..::.AllowThousands values and the number format information specified by provider. 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.
For more information about numeric formats, see the Formatting Overview topic.
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 Single value.
Protected Sub OkToSingle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToSingle.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Single
' 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 = Single.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 OkToSingle_Click(object sender, EventArgs e)
{
string locale;
float 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 = Single.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