Convert String to Double in C#

Hi Programmers, welcome to ozanecare. the new post to let you know three important ways to convert string into double. we can convert string into with the help of Convert.ToDouble method , Double.Parse method or double.TryParse method. Lets see the codes.

1.By Using Convert.ToDouble method.see below image.

2.By Using Double.Parse method.see below image.

3.By Using double.TryParse method.see below image.

Combined all methods into one place. see below codes.

using System;
namespace ConsoleApp1
    class Program
        static void Main(string[] args)
	     // 1st way
            Console.WriteLine(Convert.ToDouble("56.99"));                   Console.WriteLine(Convert.ToDouble("56.99").GetType()); 

            // 2nd Way
            String s = "45";
            double d = Double.Parse(s);
            // 3rd way
	    string s1 = "10";
            string s2 = "20";
            double n1, n2;
            double.TryParse(s1, out n1);
            double.TryParse(s2, out n2);
            Console.WriteLine("After Converting to Double {0} and {1} ", n1, n2);      

Happy Coding …Thanks.

Post Author: adama