locked
Converting "String" to "Double". RRS feed

  • Question

  • Hi,

    Can someone tell me how do i convert a "String" variable to a "Double" variable?

    Thanks!

    Monday, November 20, 2006 3:30 PM

Answers

  • Try using the double.parse() method.

            string d = "1234.5";

            double m = double.Parse(d);

     

     

    Monday, November 20, 2006 3:44 PM
  • in addition, in .NET 2.0 there is a TryParse method. This allows you to check whether or not the string supplied is a double "compatible" type, in other words, checks to see if the value given can be converted to double. This is the preferred approach since doing a direct Parse can cause an exception if the string is not in the required format. If you know it is definately a double value from a string then yes use Parse. Example of using TryParse:

     

    double theValue = 0;

    if (double.TryParse("123.2", out theValue))

    {

       //value was converted to double and result stored in "theValue"

    }

    Monday, November 20, 2006 7:29 PM

All replies

  • Try using the double.parse() method.

            string d = "1234.5";

            double m = double.Parse(d);

     

     

    Monday, November 20, 2006 3:44 PM
  • in addition, in .NET 2.0 there is a TryParse method. This allows you to check whether or not the string supplied is a double "compatible" type, in other words, checks to see if the value given can be converted to double. This is the preferred approach since doing a direct Parse can cause an exception if the string is not in the required format. If you know it is definately a double value from a string then yes use Parse. Example of using TryParse:

     

    double theValue = 0;

    if (double.TryParse("123.2", out theValue))

    {

       //value was converted to double and result stored in "theValue"

    }

    Monday, November 20, 2006 7:29 PM