Question #6537   Submitted by Answiki on 09/03/2022 at 06:49:57 AM UTC

How to convert a floating point to ASCII in C?

Answer   Submitted by Answiki on 09/21/2022 at 06:14:56 PM UTC

In C, the best way to convert a floating point into an ASCII string is to use the sprintf() function:

int sprintf( char * buffer, const char *format, ... );     


The following example converts a floating point number (pi) to string (str):

#include <stdio.h>

int main()
{
    // Floatting point number
    float pi = 3.1415;
    // String for ASCII value of PI
    char str[7];
    
    // Convert and display floatting point to string
    sprintf(str, "%.4f", pi);
    printf ("pi = %s\n", str);
    return 0;
}


2 events in history
Answer by Answiki on 09/21/2022 at 06:14:56 PM

In C, the best way to convert a floating point into an ASCII string is to use the sprintf() function:

int sprintf( char * buffer, const char *format, ... );     


The following example converts a floating point number (pi) to string (str):

#include <stdio.h>

int main()
{
    // Floatting point number
    float pi = 3.1415;
    // String for ASCII value of PI
    char str[7];
    
    // Convert and display floatting point to string
    sprintf(str, "%.4f", pi);
    printf ("pi = %s\n", str);
    return 0;
}


Question by Answiki 09/03/2022 at 06:49:57 AM
How to convert a floating point to ASCII in C?
# ID Query URL Count

Icons proudly provided by Friconix.