Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

float to string in C++?

Posted on 2001-06-05
4
Medium Priority
?
2,345 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

604 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