How to Convert Floating Point Number to String in C Language

https://‮w‬ww.lautturi.com
How to Convert Floating Point Number to String in C Language

the output shows on the display screen only using printf.
How to convert a floating point number to a string of a certain format?
We can use the sprintf function to save the results to an array of strings.

/*
Example:convert float to string  in C language
*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float f = 1.23456789;
    char c[20];
	
	// Rounded to 5 decimal places
    sprintf(c, "%.5f", f);
    printf(c);
    printf("\n");
}

output:

1.23457
Created Time:2017-08-28 15:11:58  Author:lautturi