• C

trabslate

I'm working whit bioscom and i´m reading in the screen. But it is a character. How can I translate character to float?

thanks.
juancarlosAsked:
Who is Participating?
 
sgantaConnect With a Mentor Commented:
Hi
You read it as a character string. You store into some character array(string).
Use this function which converts character to float.

float get_float(char test[10]) // or you can give char *test
{
   int j,len,i=0,intpart;
   char ch,ipart[4];
   float num,k,flt;  
   len = strlen(test);
   j=1;
   ipart[0] = '0';
   while (i<len && test[i] != '.')
   {
      ipart[j] = test[i];
      j++;
      i++;
   }

   // Now integer part is get selected
   ipart[j] = '\0';
   intpart = atoi(ipart); /* Or you can use your method to convert into
                             Integer Value */

   num = intpart; /* Or num = (float) intpart; */

   i++;
   k = 1;
   while ( i<len)
   {
     flt = test[i]-48;  /* Or flt = (float)(test[i]-48); */
     k /=10;
     num +=k*flt;
     i++;
   }
     
   return num; // Returns the floating number
}            
0
 
juancarlosAuthor Commented:
I'm working whit C++ 2.0 in 386
0
 
kellyjjCommented:
Is the char just a number?  ch="2"   Or is it the actual asci char?
0
 
ozoCommented:
#include <stdlib.h>
double d = atof("2");
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.

All Courses

From novice to tech pro — start learning today.