Casting a string to a double

  • Thread starter Thread starter Tina
  • Start date Start date
T

Tina

I'm trying to do this:
Double myDouble
myDouble = (double) myString

I have ensured that myString has a valid value i.e. 79.46. But the compiler
won't let me do the cast.

How can I get the value into myDouble.

Thanks,

T
 
Try this:

myDouble = Double.Parse();

Make sure to handle any possible errors of course. look it up in
documention.
 
prefer the use of double.tryparse instead because 1. it doesn't throw
2. it is culture aware

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
-------------------------------------------------------
 
Try this
System.String MyString = "123.456";

System.Double MyDouble;

System.Boolean CanBeConverted = System.Double.TryParse(MyString, out
MyDouble);
 
prefer the use of double.tryparse instead because 1. it doesn't throw

Well, that depends on whether or not you *want* it to throw an
exception if the value is invalid. On several occasions it makes
perfect sense to throw.
2. it is culture aware

Double.Parse is culture-aware as well. You can pass it an
IFormatProvider if you want to, but if you use an overload which
doesn't take one, and the culture associated with the current thread.
 
Yup, that's correct. I think the Double.Parse method evolved while I wasn't
looking from one version of the framework to the other. It now seems that
the only benefit of the tryparse over parse is that it doesn't throw.

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 
looking from one version of the framework to the other. It now seems that
the only benefit of the tryparse over parse is that it doesn't throw.

That's no benefit.
 
looking from one version of the framework to the other. It now seems
that
I look at it as, "the benefit of Parse is that it _will_ throw when there's
a problem".

When writing a class that implements a .Parse method the developer can
choose to throw different exceptions for different problems and the user of
the class can then choose an appropriate action based on the problem. The
more information the better.
 
PIEBALD said:
I look at it as, "the benefit of Parse is that it _will_ throw when there's
a problem".

When writing a class that implements a .Parse method the developer can
choose to throw different exceptions for different problems and the user of
the class can then choose an appropriate action based on the problem. The
more information the better.

Unless all you're really interested in is "has the user entered a valid
number". That's pretty common when validating input, and having to use
a try/catch for it is ugly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top