Is there any library function in KEIL C which converts a floating point into string. If not can somebody give the appropriate code.
I think you're looking for sprintf().
Which isn't Keil-specific, by the way, but part of the standard libraries.
"isn't Keil-specific, by the way, but part of the standard libraries."
But, if you're using the free evaluation version, be sure to carefully read the limitations of that version...
maybe you can write his code; for example;
- specify your string array
unsigned char str_float[20];
. -find the maximum divider, and number of digit at left side of comma
if(float_number<10) {divider=1;digit=1} if(float_number<100) {divider=10;digit=2} if(float_number<1000) {divider=100;digit=3} ... etc
.
-and how much digit you want at right side of comma, for example you want two digit from right side of comma, multiply with 100 and convert to long
long_value=(unsigned long)float_value*100; digit+=2; divider*=100;
. --and now start take digits
str_float[digit+2]="\0"; while(digit>2){ str_float[digit+1]=long_number/(divider) + "0" ; long_number/=10; divider/=10; digit--; } str_float[2]=","; str_float[1]=long_number/10+"0"; str_float[0]=long_number%10+"0";
i know this code very weak, powerless, i write this only for give an idea. for example float_value=0,54 this code fails
maybe you can write his code;
Why reinvent the wheel ?
sprintf() does the job.
sprintf() (and scanf()) are big and slow. Occasionally, for some limited applications, it can be worthwhile to write custom parsing or output that doesn't have to handle all of the data types and formatting options of the library routines.
But it's rarely worth the effort.
why worry about "sprintf() (and scanf()) are big and slow" once you have already lost any illusion of performance by using floating point with a '51?
Erik
If scanf() is big and slow, check for the availability of atof() or strtod().
If sprintf() is big and slow, check for the availability of ecvt(), fcvt() or gcvt() functions.
The scanf() and sprintf() functions are normally never implemented monolitically. They normally makes use of helper functions like the above suggestions, and they may be used directly.
By the way, don't stop using scanf() because it is large or slow. Stop using it because it is quite lousy. It has a tendancy to be dangerous. It has a tendancy to leave data in the input buffer. It only returns the number of converted arguments - not the location of the first erroneous character. It isn't always obvious how to write the formatting string, to handle non-convertable data in the input string.
"check for the availability of atof() or strtod() ... ecvt(), fcvt() or gcvt"
www.google.com/codesearch
// This code converts the given float values to a string // input argumentsto the function are 1. Float value that to be converted 2. Number of digits at the output string 3. The string variable to store the converted results
/* Perfect float to string Conversion c program */
#include <stdio.h>
void ftostr(double num, int digits,char *buff) { int i_temp = (int)num; float f_temp = num-i_temp;
char temp_s[20]; int i_len = i_temp>0 ?1:2; int ref_len = i_len; int var = i_temp; while(var) { i_len += 1; var /=10; } int temp_len = i_len; for(int i=0;i<digits;i++) { if(temp_len==ref_len) { temp_s[i_len-2-i]='0' + i_temp%10; temp_len -= 1; } else if(temp_len>ref_len) { temp_s[i_len-2-i]='0' + i_temp%10; i_temp /= 10; temp_len -= 1; } if(i_len-1 == i) { temp_s[i] = '.'; temp_len -= ref_len==2?1:0; } else if(temp_len == 0) { temp_s[i] =(int)(f_temp*10)+'0'; f_temp = f_temp *10; f_temp = f_temp - (int)f_temp; } } for(int i=0;i<=digits;i++) { buff[i] = temp_s[i]; }}
int main(){ float a = 10.2563; char tx[100]; ftostr(a,10,tx); printf("\n %s",tx);
return 0;}
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]; } }}