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 NumberFormatInfo..::.PositiveInfinitySymbol, NumberFormatInfo..::.NegativeInfinitySymbol, or NumberFormatInfo..::.NaNSymbol. Depending on the value of style, it can also take the form:
[ws][$][sign][integral-digits[,]]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws]
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 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 |
|---|
None
| The digits element only. |
AllowDecimalPoint
| The . and fractional-digits elements. |
AllowExponent
| The s parameter can also use 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. |
AllowLeadingWhite
| The ws element at the beginning of s. |
AllowTrailingWhite
| The ws element at the end of s. |
AllowLeadingSign
| The sign element at the beginning of s. |
AllowTrailingSign
| The sign element at the end of s. |
AllowParentheses
| The sign element in the form of parentheses enclosing the numeric value. |
AllowThousands
| The , element. |
AllowCurrencySymbol
| The $ element. |
Currency
| All. The s parameter cannot represent a hexadecimal number or a number in exponential notation. |
Float
| The ws element at the beginning or end of s, sign at the beginning of s, and the . symbol. The s parameter can also use exponential notation. |
Number
| The ws, sign, , and . elements. |
Any
| All styles, except s cannot represent a hexadecimal number. |
The s parameter is parsed using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. For more information, see CurrentInfo.
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.