Solved

coding of float to string

Posted on 2004-10-02
13
724 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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.

746 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

13 Experts available now in Live!

Get 1:1 Help Now