C Language Development Kit/Functions/FToA

From OptiWiki

Jump to: navigation, search
Description Converts a float into a readable string.
Syntax
char * ftoa(char * buffer, float a);
Arguments
char * buffer
Character buffer into which the result string is copied. The maximum number of characters that will be stored in the buffer is 12.
float a
32-bit floating point value that is to be converted into a string.
Returns Character pointer to the result string of the converted value
Remarks This functions convert a floating point variable in a same way as the %g type format specifier of printf would.

This means it will either be converted to the form [-]dddd.dddd, where dddd is one or more decimal digits or to the scientific notation with an 'e', depending on which of the two is more compact.

Make sure the result string is atleast 12 characters in size.

Example
#include <stdio.h>
#include <math.h>
#include <time.h>
#include "lib.h"
 
void main(void)
{
    double a;
    float b,c,d, e;
    char buffer[15];
 
    a = 2.5;
    b = 2.0;
    c = a*b;
    d= 0.5f;
 
    printf("\fFloat test\n");
    a = (double)c*sin(d*3.1f);
    printf("%s\n",dftoa(buffer,a));
    e = (float)a/d;
    printf("%s\n",ftoa(buffer,e));
 
    while(1)
        idle();
}


Views
Personal tools