# atof function returns 0/-1.#INF0000000

Using the atof() function, I try convert both a CString and a char to a double. When I try to convert a number with 00 after the decimal point, ie "10"/"10.00" the function sometimes returns either 0 or -1.#INF00000, and sometimes the real values, ie 10.
What is my problem?
Thanks,
Alon
###### Who is Participating?

Commented:
Your problem is 'atof()' - it is not _very_ reliable (and returns only float, and not double). I'd suggest 'strtod()' instead, which provides a more sophisticated way of error checking, e.g.

double d;
char* pc;
char* pszNum = "10.0";
CString strNum( "11.0");

d = strtod ( pszNum, &pc);

if (*pc)
{
// conversion ended with sth. else than a null terminator, error
}

d = strtod ( ( LPCTSTR) strNum, &pc); // operator LPCTSTR() is overloaded!

if (*pc)
{
// conversion ended with sth. else than a null terminator, error
}

d = strtod ( strNum->GetBuffer ( 0), &pc);

if (*pc)
{
// conversion ended with sth. else than a null terminator, error
}

0

Commented:
I'd need to see a little of your code, but one thing you can not do is:

CString str = "10.0";
double x = atof(str);

What you can do is:

CString str = "10.0";
double x = atof( (const char*)str);
0

Author Commented:
I have tried to change the atof function into strtod, but got the same problem. I use the second example with the "(LPCTSTR)".
The variable I try to connvert is a DB field, it keeps a balance, and it has to be character type.
When I start the system, it works just fine, but then when I tried to update the field with the value "10", the strtod function returns 0. The *pc is null.

Thanks,
Alon
0

Commented:
Try displaying the string you want to convert using 'AfxMessageBox()' - perhaps there's really "0" in it... If not, we'll have to take a closer look ;-)
0

Author Commented:
i put a breakpoint just before the strtod(), and saw that the value of the string is "10"
0

Commented:
//did you
#include <stdlib.h>
//or declare
double atof (const char *nptr);
0

Commented:
Could you post the code?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.