Export (0) Print
Expand All

Double.Parse Method (String)

Updated: May 2009

Converts the string representation of a number to its double-precision floating-point number equivalent.

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

public static double Parse(
	string s
)

Parameters

s
Type: System.String

A string containing a number to convert.

Return Value

Type: System.Double
A double-precision floating-point number equivalent to the numeric value or symbol specified in s. Because of differences in precision, the return value may not be exactly equal to s, and for values of s that are less than Epsilon, the return value may also differ depending on processor architecture. For more information, see the Remarks section of Double.

ExceptionCondition
ArgumentNullException

s is null.

FormatException

s is not a number in a valid format.

OverflowException

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

The s parameter can contain the current culture's NumberFormatInfo.PositiveInfinitySymbol, NumberFormatInfo.NegativeInfinitySymbol, NumberFormatInfo.NaNSymbol, or a string of 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.

Element

Description

ws

A series of white space characters.

sign

A negative sign or positive sign symbol. Only a leading sign can be used.

integral-digits

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.) Integral-digits can be absent if there are fractional-digits.

,

A culture-specific thousands separator symbol.

.

A culture-specific decimal point symbol.

fractional-digits

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

E

An uppercase or lowercase character 'e', indicating exponential (scientific) notation.

exponential-digits

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

The s parameter is interpreted using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags. This means that white space and thousands separators are allowed, for example, while currency symbols are not. For finer control over which style elements are permitted in s for the parse operation to succeed, call the Double.Parse(String, NumberStyles) or the Double.Parse(String, NumberStyles, IFormatProvider) method.

The s parameter is interpreted using the formatting information in a NumberFormatInfo object that is initialized for the current thread culture. For more information, see CurrentInfo. To parse a string using the formatting information of some other culture, call the Double.Parse(String, IFormatProvider) or Double.Parse(String, NumberStyles, IFormatProvider) method.

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.

   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.

The following code example illustrates the use of Parse, taking a String as a parameter:

	public class Temperature {
		// Parses the temperature from a string in form 
		// [ws][sign]digits['F|'C][ws] 
		public static Temperature Parse(string s) {
			Temperature temp = new Temperature();

			if( s.TrimEnd(null).EndsWith("'F") ) {
				temp.Value = Double.Parse( s.Remove(s.LastIndexOf('\''), 2) );
			}
			else if( s.TrimEnd(null).EndsWith("'C") ) {
				temp.Celsius = Double.Parse( s.Remove(s.LastIndexOf('\''), 2) );
			}
			else {
				temp.Value = Double.Parse(s);
			}

			return temp;
		}

		// The value holder 
		protected double m_value;

		public double Value {
			get {
				return m_value;
			}
			set {
				m_value = value;
			}
		}

		public double Celsius {
			get {
				return (m_value-32.0)/1.8;
			}
			set {
				m_value = 1.8*value+32.0;
			}
		}
	}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

May 2009

Expanded the Return Value section.

Content bug fix.

March 2009

Expanded the Remarks section.

Content bug fix.

Community Additions

ADD
Show:
© 2014 Microsoft