Solved

float to string in C++?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
Handle Exceptions during instantiation 28 411
What does std::atomic give me? 7 113
Would like to move button in a function 3 72
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

920 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

15 Experts available now in Live!

Get 1:1 Help Now