C# Double Parse(String)

Description

Double Parse(String) converts the string representation of a number to its double-precision floating-point number equivalent.

Syntax

Double.Parse(String) has the following syntax.


public static double Parse(
  string s
)

Parameters

Double.Parse(String) has the following parameters.

  • s - A string that contains a number to convert.

Returns

Double.Parse(String) method returns A double-precision floating-point number that is equivalent to the numeric value or symbol specified in s.

Example

In addition, attempting to parse the string representation of either MinValue or MaxValue throws an OverflowException, as the following example illustrates.


using System;//  w  w  w.j  a va 2 s  . com
public class MainClass{
  public static void Main(String[] argv){  
   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 code above generates the following result.

Example 2

Read a double value from keyboard and parse


using System;// w  w  w.j av a2s  . c o m

public class MainClass{
  public static void Main(String[] argv){  

      bool done = false;
      string inp;
      do {
         Console.Write("Enter a real number: ");
         inp = Console.ReadLine();
         try {
            double  d = Double.Parse(inp);
            Console.WriteLine("You entered {0}.", d.ToString());
            done = true;
         } 
         catch (FormatException) {
            Console.WriteLine("You did not enter a number.");
         }
     catch (ArgumentNullException) {
            Console.WriteLine("You did not supply any input.");
         }
         catch (OverflowException) {
             Console.WriteLine("The value you entered, {0}, is out of range.", inp);      
         }
      } while (!done);

  }
}

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