getting a double from an AnsiString

snellejelle
snellejelle used Ask the Experts™
on
I'm quite new at C++, and working with borland builder 5.5.
I am trying to get a float from an Edit, but the Edit returns an AnsiString when i use Edit2->Text. Is it possible to cast it to a double, or should i try something different? (and what?)
Thanx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
EOL

Commented:
with AnsiString you mean a cString (or M$ calls them LPCSTR )

You do you can get a float from the string with the function float atof( char * ). example:
float test = atof("1234.04");

If you mean std::string with AnsiString it's not that easy, but much nicer. For formatted input and output you need a stream. for string you need the stringstream.

#include <string>
#include <sstream>
stringstream ss;
ss << string( "1234.05" );
float test;
ss >> test;

As you can see you can construct the string from the cString easely.
Commented:
Hello snellejelle
Yes it is possible, just use .toDouble(),
here is a short example:
(tested under Borland Builder 5 Prof.)

Edit1->Text="1,234";

AnsiString aVar = Edit1->Text;
     
        //Konverts AnsiString to Double
float bVar = aVar.ToDouble();
         


You will see bVar is now 1.234.

@EOL:  AnsiString is not the same as cString
       AnsiString ->Borland
       cString    ->M$


Greetz Sdyx
EOL

Commented:
Yes, there is a CString class by M$, which nobody uses. There is however also the c-String which refers to a char array with trailing zero ( hence the name c-String ). From my usage it should be obious what I meant with cString.

Intresting, didn't know Borland has it's own String class. I would use STL instead, but that's up to you of course.

Author

Commented:
thnx a lot!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial