This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

code for converting float to string

Is there any library function in KEIL C which converts a floating point into string. If not can somebody give the appropriate code.

Parents
  • void float_to_char(char *total_str,float value)
    {
    int ipart=(int)value; //Extracting interger part only
    float fpart=value-(int)value; //Extracting floating value only
    int reversed=0;
    int i=0;
    char istring[100];
    if(ipart==0)
    {
    istring[0]='0';
    }
    else
    {
    while(ipart>0)
    {
    reversed=reversed*10 + ipart%10; //Reversing integer
    ipart=ipart/10;
    }
    }
    while(reversed>0)
    {
    istring[i]=reversed%10+'0'; //Adding digits to string array
    reversed=reversed/10;
    i++;
    }
    char fstring[100];
    int k = 0;
    float x= fpart;
    fstring[k]='.'; //Adding . as decimal point
    k++;
    for(int j=0;j<6;j++)
    {
    x=x*10;
    fstring[k]='0'+(int)x;
    x=x-(int)x;
    k++;
    }

    int len = strlen(istring); //Length of integer string
    for(int i=0;i<100;i++)
    {
    if(istring[i]!='\0')
    {
    total_str[i]=istring[i]; //Grouping integer string and floating value string
    }
    else if(fstring[i]!='\0')
    {
    total_str[i]=fstring[i-len];
    }
    }
    }

Reply
  • void float_to_char(char *total_str,float value)
    {
    int ipart=(int)value; //Extracting interger part only
    float fpart=value-(int)value; //Extracting floating value only
    int reversed=0;
    int i=0;
    char istring[100];
    if(ipart==0)
    {
    istring[0]='0';
    }
    else
    {
    while(ipart>0)
    {
    reversed=reversed*10 + ipart%10; //Reversing integer
    ipart=ipart/10;
    }
    }
    while(reversed>0)
    {
    istring[i]=reversed%10+'0'; //Adding digits to string array
    reversed=reversed/10;
    i++;
    }
    char fstring[100];
    int k = 0;
    float x= fpart;
    fstring[k]='.'; //Adding . as decimal point
    k++;
    for(int j=0;j<6;j++)
    {
    x=x*10;
    fstring[k]='0'+(int)x;
    x=x-(int)x;
    k++;
    }

    int len = strlen(istring); //Length of integer string
    for(int i=0;i<100;i++)
    {
    if(istring[i]!='\0')
    {
    total_str[i]=istring[i]; //Grouping integer string and floating value string
    }
    else if(fstring[i]!='\0')
    {
    total_str[i]=fstring[i-len];
    }
    }
    }

Children
No data