Solved

float to string in C++?

Posted on 2001-06-05
4
2,287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

739 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