C# Double TryParse(String, Double)

Description

Double TryParse(String, Double) converts the string representation of a number to its double-precision floating-point number equivalent. A return value indicates whether the conversion succeeded or failed.

Syntax

Double.TryParse(String, Double) has the following syntax.


public static bool TryParse(
  string s,
  out double result
)

Parameters

Double.TryParse(String, Double) has the following parameters.

  • s - A string containing a number to convert.
  • result - When this method returns, contains the double-precision floating-point number equivalent of the s parameter, if the conversion succeeded, or zero if the conversion failed. The conversion fails if the s parameter is null or String.Empty, is not a number in a valid format, or represents a number less than MinValue or greater than MaxValue. This parameter is passed uninitialized.

Returns

Double.TryParse(String, Double) method returns true if s was converted successfully; otherwise, false.

Example

The following example uses the TryParse(String, Double) method to convert the string representations of numeric values to Double values. It assumes that en-US is the current culture.


using System;/*ww  w . j  a v a 2s. c  o  m*/

public class Example
{
   public static void Main()
   {
      string[] values = { "1,234.57", "$1,234.57", "-1.643e6", 
                          "-123456789012345662", "123AE6", 
                          null, String.Empty, "ABCDEF" };
      double number;

      foreach (var value in values) {
         if (Double.TryParse(value, out number)) 
            Console.WriteLine("'{0}' --> {1}", value, number);
         else
            Console.WriteLine("Unable to parse '{0}'.", value);      
      }   
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version