Parse Method (String, IFormatProvider)
Collapse the table of content
Expand the table of content

Single.Parse Method (String, IFormatProvider)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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)

public static float Parse(
	string s,
	IFormatProvider provider


Type: System.String
A string that contains a number to convert.
Type: System.IFormatProvider
An object that supplies culture-specific formatting information about s.

Return Value

Type: System.Single
A single-precision floating-point number equivalent to the numeric value or symbol specified in s.


s is null.


s is not a number in a valid format.


s represents a number less than MinValue or greater than MaxValue.

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 is interpreted using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags. The s parameter can contain NumberFormatInfo.PositiveInfinitySymbol, NumberFormatInfo.NegativeInfinitySymbol, or NumberFormatInfo.NaNSymbol for the culture specified by provider, or it can contain a string of the form:


Optional elements are framed in square brackets ([ and ]). Elements that contain the term "digits" consist of a series of numeric characters ranging from 0 to 9.




A series of white-space characters.


A negative sign symbol (-) or a positive sign symbol (+).


A series of digits ranging from 0 to 9 that specify 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. The integral-digits element can be absent if the string contains the fractional-digits element.


A culture-specific decimal point symbol.


A series of digits ranging from 0 to 9 that specify the fractional part of the number.


The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation.


A series of digits ranging from 0 to 9 that specify an exponent.

For more information about numeric formats, see the Formatting Types topic.

The provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific formatting information. When the Parse(String, IFormatProvider) method is invoked, it calls the provider parameter's GetFormat method and passes it a Type object that represents the NumberFormatInfo type. The GetFormat method then returns the NumberFormatInfo object that provides information about the format of the s parameter. There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

  • You can pass a CultureInfo object that represents the culture that supplies formatting information. Its GetFormat method returns the NumberFormatInfo object that provides numeric formatting information for that culture.

  • You can pass the actual NumberFormatInfo object that provides numeric formatting information. (Its implementation of GetFormat just returns itself.)

  • You can pass a custom object that implements IFormatProvider. Its GetFormat method instantiates and returns the NumberFormatInfo object that provides formatting information.

If provider is null or a NumberFormatInfo cannot be obtained, the formatting information for the current system culture is used.

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 illustrates how to use the Parse(String, IFormatProvider) method overload to parse a string. The method first attempts to parse the string by using the current culture. If the parse operation fails, it attempts to parse the string by using a neutral culture. If this parse operation fails, it attempts to parse the string by using the invariant culture. Note that the Single.TryParse(String, NumberStyles, IFormatProvider, Single) method is actually better suited for parsing an unknown string, since it does not require that the routine handle repeated exceptions.

public static float GetSingle(string value)
   float number;
   CultureInfo culture = null;

   // Throw exception if string is empty.
   if (String.IsNullOrEmpty(value))
      throw new ArgumentNullException("The input string is invalid.");

   // Determine if value can be parsed using current culture.
      culture = CultureInfo.CurrentCulture;
      number = float.Parse(value, culture);
      return number;
   catch {}
   // If Parse operation fails, see if there's a neutral culture.
   try {
      culture = culture.Parent;
      number = float.Parse(value, culture);
      return number;
   catch {}
   // If there is no neutral culture or if parse operation fails, use
   // the invariant culture.
   culture = CultureInfo.InvariantCulture;
   try {
      number = float.Parse(value, culture);
      return number;
   // All attempts to parse the string have failed; rethrow the exception.
   catch (FormatException e)
      throw new FormatException(String.Format("Unable to parse '{0}'.", value), 

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2017 Microsoft