By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,780 Members | 1,523 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,780 IT Pros & Developers. It's quick & easy.

Casting a string to a double

P: n/a
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
May 18 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Try this:

myDouble = Double.Parse();

Make sure to handle any possible errors of course. look it up in
documention.

"Tina" <ti**********@nospammeexcite.com> wrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
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

May 18 '06 #2

P: n/a
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
-------------------------------------------------------

"Glen Martin" <He*********@newsgroups.nospam> wrote in message
news:uC**************@TK2MSFTNGP04.phx.gbl...
Try this:

myDouble = Double.Parse();

Make sure to handle any possible errors of course. look it up in
documention.

"Tina" <ti**********@nospammeexcite.com> wrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
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


May 19 '06 #3

P: n/a
Try this
System.String MyString = "123.456";

System.Double MyDouble;

System.Boolean CanBeConverted = System.Double.TryParse(MyString, out
MyDouble);
"Tina" <ti**********@nospammeexcite.com> a écrit dans le message de news:
%2******************@TK2MSFTNGP02.phx.gbl...
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

May 19 '06 #4

P: n/a
<"Alvin Bruney" <www.lulu.com/owc>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 19 '06 #5

P: n/a
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
-------------------------------------------------------

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Alvin Bruney" <www.lulu.com/owc>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 19 '06 #6

P: n/a
> 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.
May 19 '06 #7

P: n/a
PIEBALD <PI*****@discussions.microsoft.com> wrote:
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.


Well, it is in some circumstances - but it entirely depends on the
situation.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 19 '06 #8

P: n/a
> > > 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.


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.
May 19 '06 #9

P: n/a
PIEBALD <PI*****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 19 '06 #10

P: n/a
Ugly, schmugly, who's going to see it? And it's good practice for the kiddies.
May 19 '06 #11

P: n/a
PIEBALD <PI*****@discussions.microsoft.com> wrote:
Ugly, schmugly, who's going to see it?
Whoever's maintaining the code.
And it's good practice for the kiddies.


???

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 20 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.