Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Converting a String into double?

Posted on 2004-08-10
5
Medium Priority
?
10,986 Views
Last Modified: 2010-04-24
How is a String converted into double in Visual C++ .NET?
0
Comment
Question by:judico
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 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