Single.Parse Method (String, NumberStyles)
Converts the string representation of a number in a specified style to its single-precision floating-point number equivalent.
Assembly: mscorlib (in mscorlib.dll)
- Type: System.String
A string that contains a number to convert.
Return ValueType: System.Single
A single-precision floating-point number that is equivalent to the numeric value or symbol specified in s.
s is a null reference (Nothing in Visual Basic).
s is not a number in a valid format.
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:
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 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.
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.
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
XNA FrameworkSupported in: 3.0, 2.0, 1.0
Portable Class LibrarySupported in: Portable Class Library
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8