coding of float to string

Posted on 2004-10-02
Last Modified: 2010-05-18
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 ?

Question by:onsight
  • 2
  • 2
  • 2
  • +4
LVL 86

Accepted Solution

jkr earned 84 total points
ID: 12209095
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 );
LVL 11

Expert Comment

ID: 12209811
there seems to be an implementation in line of atoa() here

LVL 11

Expert Comment

ID: 12209815
oops read that as
...... in line of itoa() .....
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12209936
Have a look to this source code. Maybe you can taylor to your needs:

Expert Comment

ID: 12210457
You can also use strtod() from stdlib.h
Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.


Assisted Solution

ankuratvb earned 83 total points
ID: 12210479
LVL 22

Assisted Solution

grg99 earned 83 total points
ID: 12211841
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

LVL 22

Expert Comment

ID: 12221069
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.


Expert Comment

ID: 12237179
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


Expert Comment

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

- MA

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now