Export (0) Print
Expand All

Double.Parse Method (String, NumberStyles, IFormatProvider)

Converts the string representation of a number in a specified style and culture-specific format to its double-precision floating-point number equivalent.

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

public static double Parse (
	string s,
	NumberStyles style,
	IFormatProvider provider
)
public static double Parse (
	String s, 
	NumberStyles style, 
	IFormatProvider provider
)
public static function Parse (
	s : String, 
	style : NumberStyles, 
	provider : IFormatProvider
) : double
Not applicable.

Parameters

s

A string containing a number to convert.

style

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

provider

An IFormatProvider object that supplies culture-specific formatting information about s.

Return Value

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

Exception typeCondition

ArgumentNullException

s is a null reference (Nothing in Visual Basic).

FormatException

s is not a numeric value.

ArgumentException

style is not a NumberStyles value.

-or-

style is the AllowHexSpecifier value.

OverflowException

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

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 PositiveInfinitySymbol, NegativeInfinitySymbol, or 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]

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.

Element

Description

ws

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 CurrencyNegativePattern and CurrencyPositivePattern properties of the System.Globalization.NumberFormatInfo object returned by the IFormatProvider.GetFormat method of the provider parameter. The currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.

sign

A negative sign or 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 also be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.

integral-digits

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

,

A culture-specific thousands separator symbol. The thousands separator of the culture defined by provider can appear in s if style includes the NumberStyles.AllowThousands flag.

.

A culture-specific decimal point symbol. The decimal point symbol of the culture defined by provider can appear in s if style includes the NumberStyles.AllowThousands flag.

fractional-digits

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

E

An uppercase or lowercase character 'e', indicating exponential (scientific) notation. The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.

exponential-digits

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 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 integral-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 integral-digitsEexponential-digits; additional flags are needed to successfully parse strings in exponential notation 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 provider parameter is a IFormatProvider implementation, such as a NumberFormatInfo or CultureInfo object. The provider parameter supplies culture-specific information used in parsing. If provider is a 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 [<topic://cpConFormattingOverview>] 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 illustrates the use of the Parse method to assign several string representations of temperature values to a Temperature object.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft