Solved

coding of float to string

Posted on 2004-10-02
13
728 Views
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 ?

Regards
Martin
0
Comment
Question by:onsight
  • 2
  • 2
  • 2
  • +4
13 Comments
 
LVL 86

Accepted Solution

by:
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 );
}
0
 
LVL 11

Expert Comment

by:avizit
ID: 12209811
there seems to be an implementation in line of atoa() here
http://gnomic.stanford.edu/pub/SAPS.SSPA/SAPS.SSPA/Src/ftoa.c

0
 
LVL 11

Expert Comment

by:avizit
ID: 12209815
oops read that as
...... in line of itoa() .....
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12209936
Have a look to this source code. Maybe you can taylor to your needs:
http://www.italios.it/os2/stdlib_8c-source.html
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 12210457
You can also use strtod() from stdlib.h
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 83 total points
ID: 12210479
0
 
LVL 22

Assisted Solution

by:grg99
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

0
 
LVL 22

Expert Comment

by:grg99
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.

.
0
 

Expert Comment

by:maheshstms
ID: 12237179
hi
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*/
f=f-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);
*/
for(k=0;f>=0.0000;k++){
    f=f*10;
    j[k]=f;
    f=f-j[k];
    /* use itoa and convert j[k] to a string temp*/
    img[k]=temp[0];
}
/*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

0
 
LVL 1

Expert Comment

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

- MA
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

860 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