Solved

float to string in C++?

Posted on 2001-06-05
4
2,259 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

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 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