• C

coding of float to string

I want to have a C Source code for converting floating point numbers to a string?
This source should not use any sprintf, even floor() should not be used !
The code should run on a small embedded system where memory is limited
It would be very nice to feature also the number of decimals, like printf ("%.2f",x) does.

Any ideas or code-snippets ?

Who is Participating?
jkrConnect With a Mentor Commented:
Can you use '_gcvt()'? E.g.

/* _GCVT.C: This program converts -3.1415e5
 * to its string representation.

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

void main( void )
   char buffer[50];
   double source = -3.1415e5;
   _gcvt( source, 7, buffer );
   printf( "source: %f  buffer: '%s'\n", source, buffer );
   _gcvt( source, 7, buffer );
   printf( "source: %e  buffer: '%s'\n", source, buffer );
there seems to be an implementation in line of atoa() here

oops read that as
...... in line of itoa() .....
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

Jaime OlivaresSoftware ArchitectCommented:
Have a look to this source code. Maybe you can taylor to your needs:
You can also use strtod() from stdlib.h
grg99Connect With a Mentor Commented:
It's a bit tricky to do the conversion correctly, with rounding at the proper column, and without accumulating error bits along the way.

Here's roughly how to do it:

double X, Pow;  int Digit, signif;

if( x < 0.0 ) { print("-"); X = -X; }

Pow = 1.0;

while( Pow < X ) Pow *= 10.0;

do {
        Digit =  (int)  X / Pow;
        print( Digit + '0' );
        X = X - Pow * Digit;
        Pow  /= 10.0;
     } while( X >= 1.0 );

print( "." );

X *= 10.0;  signif = 9999;

while( X > 0.0 && signif-- > 0 ) {
        Digit =  (int)  X;
        if( signif > 99 && Digit != 0 ) signif = (int) (sizeof( X ) * 2.5 );
        print( Digit + '0' );
        X     = X - (double) Digit;
        X   *= 10.0;


Of course you can add extra loop counters if you want a certain number of digits.
And you should add 0.5 in the least significant column to round things properly, otherwise half of the time you'll get 0.19999999 instead of 0.2

Oh, and for safety's sake, you better add lots of if()'s to check for the input data being Not A Number, or Infinite, or Indefinite, or denormalized, or ......

Lotsa gotchas in floating-point output!

{ The old UCSD Pascal system had a bad bug in it's floating-point output routine-- if you asked for a very large number of digits the printed output went totally bezerk,
printing lots of totally gibberishy digits, even the significant ones got garbled.}  And of course the old Windows calculator IIRC had trouble displaying numbers near 0.01.

i used c long before and suppose this will help u

float f=101.0123 /*be ur value*/
int i,k,j[6];
char real[5],img[6],temp[1];
i=f;/* u get the real part in i*/
/* use itoa to convert i to a string value and store it in real
* i forgot the syntax any how i think it is                                 * atoi(char *,base,value);
    /* use itoa and convert j[k] to a string temp*/
/*concatinate real with a "."
* concatinate the result with img*/
/* now u get the output of float in string*/

my only doubt is if ito work in emb. if not use ur own switch casse logic

good luck

Unix supports  ecvt, fcvt, gcvt functions ..
pls check the man pages if ya working on unix

- MA
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.