Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Single.Parse Method (String, NumberStyles)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Converts the string representation of a number in a specified style to its single-precision floating-point number equivalent.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Shared Function Parse (
	s As String,
	style As NumberStyles
) As Single


Type: System.String

A string that contains a number to convert.

Type: System.Globalization.NumberStyles

A bitwise combination of enumeration values that indicates the style elements that can be present in s. A typical value to specify is Float combined with AllowThousands.

Return Value

Type: System.Single

A single-precision floating-point number that is equivalent to the numeric value or symbol specified in s.

Exception Condition

s is null.


s is not a number in a valid format.


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


style is not a NumberStyles value.


style includes the AllowHexSpecifier value.

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 PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol. Depending on the value of style, it can also take the form:


Elements in square brackets ([ and ]) are optional. The following table describes each element.


A series of white-space characters. White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.


A culture-specific currency symbol. Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.


A negative sign symbol (-) or a positive sign symbol (+). The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingSign flag. Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.


A series of digits ranging from 0 to 9 that specify the integral part of the number. The integral-digits element can be absent if the string contains the fractional-digits element.


A culture-specific group separator. The current culture's group separator symbol can appear in s if style includes the NumberStyles.AllowThousands flag


A culture-specific decimal point symbol. The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.


A series of digits ranging from 0 to 9 that specify the fractional part of the number. Fractional digits can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.


The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. The value parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.


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

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 present, 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


The integral-digits element only.


The decimal point (.) and fractional-digits elements.


The "e" or "E" character, which indicates 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.


The ws element at the beginning of s.


The ws element at the end of s.


The sign element at the beginning of s.


The sign element at the end of s.


The sign element in the form of parentheses enclosing the numeric value.


The thousands separator (,) element.


The currency ($) element.


All elements. However, s cannot represent a hexadecimal number or a number in exponential notation.


The ws element at the beginning or end of s, sign at the beginning of s, and the decimal point (.) symbol. The s parameter can also use exponential notation.


The ws, sign, thousands separator (,) and decimal point (.) elements.


All elements. However, s cannot represent a hexadecimal number.

Some examples of s are "100", "-123,456,789", "123.45e+6", "+500", "5e2", "3.1416", "600.", "-.123", and "-Infinity".

The s parameter is parsed using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. To specify the culture whose formatting information is used for the parse operation, call the Parse(String, NumberStyles, IFormatProvider) overload.

Ordinarily, if you pass the Parse method a string that is created by calling the ToString method, the original Single value is returned. However, because of a loss of precision, the values may not be equal.

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 Parse(String, NumberStyles) method to parse the string representations of Single values. The example uses formatting information for the en-US culture.

Imports System.Globalization
Imports System.Threading

Module ParseStrings
   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 Single
         number = Single.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
   End Sub
End Module
' 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.   

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
© 2018 Microsoft