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
  • 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

Reply
  • 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

Children