Float to ASCII conversion

Question: How to convert float number to ASCII for displaying on LCD ?



LCD takes only ASCII values as input and displays the digit. For Displaying float numbers on LCD, first we need to convert the floating point number to array of ASCII values. For example, if we want to display 3.1416, then we need to find ASCII values for each digit. To do this, we can make use of "ftoa()" function. For the execution of this function stdlib.h must be included 


This function takes two arguments, where the first argument is the floating point number which needs to be converted. The second argument is the address of the variable in which the error code is to be saved. It returns the pointer to the array containing the ASCII data. A code snippet for the same is provided below:


#include "stdlib.h"

#include "string.h"

void main(void)
   float MyFloat;
   int Status;
   char* buf;

   MyFloat = 1.2345;
   buf = ftoa(MyFloat, &Status);


If the number is less than 0.0000001, the error code is 1. If the number is greater than 2147483520, the error code is 2. In both these cases, number will not be converted successfully.


If the number is within the range 0.0000001< |input| < 2147483520, then the number will be successfully converted and the error code will be 0. The converted ASCII values will be saved in the array pointed by the returned pointer. Using this pointer, the array of ASCII values can be accessed and sent to LCD byte by byte. Or, LCD_PrString() function can be used instead which takes pointer to string as argument.