Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit's AnsiString to Double

Status
Not open for further replies.

rzrdigo

Programmer
Jan 7, 2011
19
BR
How do i convert the number given in an Edit box to double for calculations? Only found how to convert ansistring to int:

K=StrToInt(Edit1->Text);

How do i do that but to double??
 
Is this what you want?

Code:
double dDoublePrecisionNumber;

try
{
  dDoublePrecisionNumber = Edit1->Text.ToDouble();
}
catch (...)
{

}

The try/catch are not absolutely necessary, they just help your program deal with text that is not a proper floating point value.

An alternative to the code you have above is the following:

Code:
int iInteger;

try
{
  iInteger= Edit1->Text.ToInt();
}
catch (...)
{

}

ToDouble and ToInt Methods are members of the AnsiString and UnicodeString classes, so these members are also available from many different sources (TStringGrid, TLabeledEdit, etc...).

Steve.
 
Code:
AnsiString VarStr = Edit1->Text;
double K = VarStr.ToDouble();
The above is a very simple way. You really ought to either put the above in a try...catch statement or use another function just in case VarStr cannot do the conversion.



James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Do i have to #include some other lib to be able to include those functions?
For me it gives an error when i put a fraction in the edit like:
class EConvertError with message"1.23 is not a valid floating point value
 
What do you mean by "i put a fraction in the edit"? Are you saying you typed into Edit1->Text "1/2" (or something similar)?
 
No no, i mean in the Edit Box i use a value like "1.2546", that is not an integer.What i want is to do some calculations with the numbers given in the edit boxes.
Using both codes above gives me the same error, but it works fine for integers.

i think i m missing an #include<>

or what could it be?

Error: class EConvertError with message"1.23 is not a valid floating point value"
 
Got it, had to do it like this:


k1=atof((Ek1->Text).c_str());

thanks for the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top