By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Problem converting a string to a float value

P: n/a
Hi guys,

My first post here and I'm a pascal coder doing his best to move to
C/C++ so please go easy on me ;)

I'm using the MySQL C API to pull some values from a DB. One of the
fields is a float value, but I can't seem to cast this field to a float
variable correctly:-

if(mysql_exec_sql(&mysql,query)==0) /*success*/
{
printf( "%ld Record Found\n",(long) mysql_affected_rows(&mysql));
result = mysql_store_result(&mysql);
if (result) // there are rows
{
num_fields = mysql_num_fields(result);

while ((row = mysql_fetch_row(result)))
{
float total;
total = atof(row[7]);

printf("%-10s", row[7]);
printf("%f", total);

printf("\n");
}

mysql_free_result(result);
}
}

The first one shows the correct value when I show it as a string, the
second one always seems to be "NAN".

I've done some googling around but can't seem to find the answer. I
basically want to grab this field as a float value so that I can do
some maths on it. I beleive this field is, in current form, a string.

Any help would be much appreciated.

Thanks,

Dean

Jul 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
de*********@gmail.com wrote:
Hi guys,

My first post here and I'm a pascal coder doing his best to move to
C/C++ so please go easy on me ;)

I'm using the MySQL C API to pull some values from a DB. One of the
fields is a float value, but I can't seem to cast this field to a float
variable correctly:-

if(mysql_exec_sql(&mysql,query)==0) /*success*/
{
printf( "%ld Record Found\n",(long) mysql_affected_rows(&mysql));
result = mysql_store_result(&mysql);
if (result) // there are rows
{
num_fields = mysql_num_fields(result);

while ((row = mysql_fetch_row(result)))
{
float total;
total = atof(row[7]);

printf("%-10s", row[7]);
printf("%f", total);

printf("\n");
}

mysql_free_result(result);
}
}

The first one shows the correct value when I show it as a string, the
second one always seems to be "NAN".

I've done some googling around but can't seem to find the answer. I
basically want to grab this field as a float value so that I can do
some maths on it. I beleive this field is, in current form, a string.

Any help would be much appreciated.
Just a guess: Did you forget to #include <stdlib.h>?
(By the way, strtod() is usually a better choice than atof(),
since it gives you a chance to detect, e.g., "123B" as a
string that ought to be numeric but isn't.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Jul 9 '06 #2

P: n/a
>>#include <stdlib.h><<

Oh dear, how stupid do I feel? ;)

Thanks Eric - that was it.

Thanks for the tip about atof not being the best also - will try that.

Dean

Jul 9 '06 #3

P: n/a
de*********@gmail.com writes:
Hi guys,

My first post here and I'm a pascal coder doing his best to move to
C/C++ so please go easy on me ;)
Welcome.

One of the first things you need to learn is that there's no such
thing as "C/C++" (or, more precisely, there's no agreement on what
"C/C++" might mean).

C and C++ are two different languages. They're very closely related
(C++ is almost, but not quite, a superset of C), but still quite
distinct.

If you wanted to talk about C++, the place to do so would be
comp.lang.c++. If you want to talk about C, you're in the right
place. (There was no C++ in your orginal post, other than the mention
of "C/C++", so this is meant to be informative, not a complaint.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.