Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

coding of float to string

Posted on 2004-10-02
13
Medium Priority
?
735 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
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 336 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 332 total points
ID: 12210479
0
 
LVL 22

Assisted Solution

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.
Suggested Courses

577 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