Solved

Converting a String into double?

Posted on 2004-08-10
5
10,937 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You're welcome.  Thanks for the extra points.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now