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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AnsiString to double

Status
Not open for further replies.

davman2002

Programmer
Nov 4, 2002
75
0
0
US
I am an experienced C++ Programmer but this is my first experience with Borland. I need to know how to convert a string variable to a double. This is the code that I have but it does not work

AnsiString Value1 = txtValue1->Text.ToDouble();
AnsiString Value2 = txtValue2->Text.ToDouble();

DoCalculations.Subtract(Value1,Value2);

debugger says cannot convert AnsiString to double

Please help
 
There is a function in the math.h librairy that converts a string to a double but I don't know if it is what you are looking for...

From Borland C++5.0 Help files:

Syntax

#include <math.h>
double atof(const char *s);
long double _atold(const char *s);

Description

Converts a string to a floating-point number.

atof converts a string pointed to by s to double; this function recognizes the character representation of a floating-point number, made up of the following:
An optional string of tabs and spaces
An optional sign
A string of digits and an optional decimal point (the digits can be on both sides of the decimal point)
An optional e or E followed by an optional signed integer

The characters must match this generic format:
[whitespace] [sign] [ddd] [.] [ddd] [e|E[sign]ddd]
atof also recognizes +INF and -INF for plus and minus infinity, and +NAN and -NAN for Not-a-Number.
In this function, the first unrecognized character ends the conversion.
_atold is the long double version; it converts the string pointed to by s to a long double.
The functions strtod and _strtold are similar to atof and _atold; they provide better error detection, and hence are preferred in some applications.

Return Value

atof and _atold return the converted value of the input string.
If there is an overflow, atof (or _atold) returns plus or minus HUGE_VAL (or _LHUGE_VAL), errno is set to ERANGE (Result out of range), and _matherr (or _matherrl) is not called.

Example:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
float f;
char *str = &quot;12345.67&quot;;

f = atof(str);
printf(&quot;string = %s float = %f\n&quot;, str, f);
return 0;
}

Borland C++ 5.0 Programmer's Guide
 
If you use atof you have to convert AnsiString to a character array with c_str().
Code:
float f;
AnsiString Str = &quot;12345.67&quot;;

f = atof(Str.c_str());

For AnsiString values you can also try StrToFloat. When using this it is a good idea to use it in a try ... catch clause just in case the String does not contain a usable value
Code:
double Value1, Value2; // You defined these as Strings not doubles
try
{
    // Try to convert from string to double
    Value1 = StrToFloat(txtValue1.c_str());
    Value2 = StrToFloat(txtValue2.c_str());
}
catch (...)
{
     // Warn the user about the error
}
James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
To convert an AnsiString into (long) Double the method AnsiString::ToDouble() is the right choice.

The declaration of the method is (C++Builder Help)

double __fastcall ToDouble() const;

Consequently, the method returns a DOUBLE value. What you try by

AnsiString Value# = txtValue#->Text.ToDouble();

is to assert a double value to an AnsiString! The compiler of course moans. (You could also try AnsiString1= .5; for example with the same compiler error.) - Right is that txtValue->Text is a valid AnsiString to be applied the method ToDouble on.

Therefore one solution could look like:

float value1= txtValue1->Text.ToDouble();
float value2= txtvalue2->Text.ToDouble();

DoCalculations.Subtract(value1,value2);

That' s all!

----------------------------------------------------------
Also being a beginner with C++Builder (4.0) I have a problem to apply the method FloatToStrF for the vice versa conversion AnsiString -> float. However, I could go around by using &quot;old&quot; char *, sprintf function and conversion char * -> AnsiString! - The problem here is that the compiler does not want to know the necessary constants like sffGeneral.
 
Memo2->Lines->Strings [0] = FloatToStrF(result, ffCurrency, 18, 2);

Memo2->Lines->Strings [1] = FloatToStrF(result, ffNumber, 18, 2);

Memo2->Lines->Strings [2] = FloatToStrF(result, ffExponent, 6, 2);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top