Solved

Converting a String into double?

Posted on 2004-08-10
5
10,961 Views
Last Modified: 2010-04-24
How is a String converted into double in Visual C++ .NET?
0
Comment
Question by:judico
  • 3
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 125 total points
ID: 11762596
Is this managed or unmanaged C++?

Managed version:

    System::String *strd = S"123.456";
    double val = System::Double::Parse(strd);


Unmanaged version:

    std::string strd("123.456");        // You could use regular char[] instead
    double val = ::atof(strd.c_str());
   
0
 

Author Comment

by:judico
ID: 11763021
Thanks for the reply. I still cannot do the conversion. The code is managed and I'm declaring these two arrays (having also -- static int ii ;):

static String *LeSTRING[] = new String*[ 1000 ];
static int Le __gc[] = new int __gc[ 1000 ];

and then, in a loop I'm trying the code you posted above, the way I thought it should be applied in this case:

 System::String *strd = LeSTRING[ ii ];
 double Le[ ii ] = System::Double::Parse(strd);

but I get the error messages:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2440: 'initializing' : cannot convert from 'double' to 'double []'
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2440: 'initializing' : cannot convert from 'double' to 'double []'

0
 
LVL 19

Expert Comment

by:drichards
ID: 11763149
You're trying to declare Le[ii] as a variable of type double, which won't work.  You can just take the implicit conversion from double to int.  Your code should be (assuming you've assigned actual strings into LeSTRING[]):

 System::String *strd = LeSTRING[ ii ];
 Le[ ii ] = System::Double::Parse(strd);
0
 

Author Comment

by:judico
ID: 11763293
Thank you so much for the help. It works very well.

P.S. I increased the points -- let me know if there is any problem.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11763327
You're welcome.  Thanks for the extra points.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question