473,383 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Casting a string to a double

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
11 2550
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
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
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
<"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
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
> 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
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
> > > 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
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
Ugly, schmugly, who's going to see it? And it's good practice for the kiddies.
May 19 '06 #11
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
5
by: Francois Malgreve | last post by:
in the following code: object obj = this.ViewState; string s = obj.GetType().ToString(); currentCreditLimit = (double) this.ViewState; ViewState is a StateBag object (dictionary type object)...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
8
by: Herby | last post by:
Given class B and C which inherit from class A They all override a method of the form: Add( A^ lhs, A^ rhs ); So A is abstract. So if i was defining Add for class B : B::Add( A^ lhs, A^ rhs...
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
3
by: g3000 | last post by:
I have a problem with a Visual Studio 2005 web project. I have two pages. SelectProject.aspx and ShowProject.aspx The first page ( SelectProject.aspx) has two drop down lists. After the user...
2
by: Giulio Petrucci | last post by:
Hi everybody, here's my problem: I have to dymanically build (and compile, of course) some code, from some ECMAScript function. ECMAScript variables I get are not typezed, so I should have...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
14
by: kanepart2 | last post by:
Hi guys, I am having a problem with the following code snippet:- double x = (myReader); double y = (myReader); Resulting in the follwing compilation error: Cannot implicitly convert type...
8
by: scottc | last post by:
i'm stuck and i need a little direction. i'm only getting 2 error messages. i have 2 files: import java.util.ArrayList; import java.util.*; //author public class StudentTester { ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.