Solved

float to string in C++?

Posted on 2001-06-05
4
2,242 Views
Last Modified: 2007-11-27
I am supposed to implement a platform independent type library. The development is done under VC++ 6.0, but so far I used only functions from the standard ANSI C++ library and some STL stuff. Now I have to transfer a float or a double into a string that represents the number in the ?+/-###.###E+/-####? format. Visual C++ has several functions for that (?_ecvt?, ?_fcvt?, ?_gcvt?, etc.) but none is part of the ANSI C++ standard. The ANSI C++ offers function to do the transfer the other way around: ?atof? and ?strtod?. Any ideas?
TIA
Eddie
0
Comment
Question by:Mensana
  • 3
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6156248
The simplest way is to use 'sprintf()' to format the float, e.g.

float f = 12345.789;
char acFloat [ 255];

sprintf ( acFloat, "%f", f);
0
 
LVL 86

Expert Comment

by:jkr
ID: 6156257
Ooops, this should read

float f = 12345.789;
char acFloat [ 255];

sprintf ( acFloat, "%e", f);
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6156418
>>Visual C++ has several functions for that (?_ecvt?, ?
>>_fcvt?, ?_gcvt?, etc.) but none is part of the
>>ANSI C++ standard

Well, leave out the prepending underscore, and they are ;-)

ECVT(3)             Linux Programmer's Manual             ECVT(3)


NAME
       ecvt,  fcvt - convert a floating-point number to a string.

SYNOPSIS
       #include <stdlib.h>

       char *ecvt(double number, int  ndigits,  int  *decpt,  int
       *sign);

       char  *fcvt(double  number,  int  ndigits, int *decpt, int
       *sign);


So, you just could

/* ECVT.C: This program uses _ecvt to convert a
 * floating-point number to a character string.
 */

#ifdef WIN32 // always defined on current MS systems
#define ecvt _ecvt
#endif

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

void main( void )
{
   int     decimal,   sign;
   char    *buffer;
   int     precision = 10;
   double  source = 3.1415926535;

   buffer = ecvt( source, precision, &decimal, &sign );
   printf( "source: %2.10f   buffer: '%s'  decimal: %d  sign: %d\n",
           source, buffer, decimal, sign );
}


0
 
LVL 1

Author Comment

by:Mensana
ID: 6156545
Hmm! This is weird. I looked for them in the Herbert Schildt?s ?C++ - The Complete Reference? book and also in my 1996 ANSI C++ draft (electronic version). Couldn?t find a reference to them. Even the MSDN specifies that these are not ANSI functions, but I tried to compile the code without placing the prefixing underscore and the compiler didn?t complain. I?ll give you credit for that.
Anyway, the other solution will solve my problem.
Thanks,
E
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

19 Experts available now in Live!

Get 1:1 Help Now