jeremiah
Joined: 20 Jul 2010 Posts: 1353
|
|
Posted: Wed Apr 29, 2020 1:11 pm |
|
|
You can use sprintf() to "print" the float to a string, but you have to ensure the character array is long enough to hold the result + null character.
Quote: |
sprintf( )
Syntax:
sprintf(string, cstring, values...);
bytes=sprintf(string, cstring, values...)
Parameters:
string - is an array of characters.
cstring - is a constant string or an array of characters null terminated.
values - are a list of variables separated by commas. Note that format specifies do not work in ram band strings.
Returns:
Bytes is the number of bytes written to string.
Function:
This function operates like printf() except that the output is placed into the specified string. The output string will be terminated with a null. No checking is done to ensure the string is large enough for the data. See printf() for details on formatting
|
Code: |
char str[20];
sprintf(str,"%f",val);
|
I have also provided a "poorman's" version of snprintf() if you want to tell the function the size of your character array to avoid overrunning memory:
https://www.ccsinfo.com/forum/viewtopic.php?t=53652&highlight=snprintf
Code: |
#include "snprintf.h"
...
char str[20];
snprintf(str,20,"%f",val);
|
|
|