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

double to String in c++ linux

Posted on 2004-08-03
Last Modified: 2007-12-19
I want to convert double to string in C++(linux) I know that there is  _ecvt function,
but for only interger that means if I have 10.5 the out put will be 10 only and also I must specifiy the long of number(how many digit)
void main()
         double test=4.125;
         double test;
         char *result;
         int decimalpt, sign;
         cout<<" Enter a number : " ;
          printf ("\n Input was %g \n", test);
                 // so here I must menstion 3
         result = _ecvt(test, 3, &decimalpt, &sign);
         printf (" \n Result string is '%s'\n", result);
         // strFinalVal contains 4.125
what I want I only want to convert from double to string in any way.
Thank You  
Question by:albusaidi

Expert Comment

ID: 11703034
why don't you use sprintf?

Expert Comment

ID: 11703393
Ok... I would say using sprintf.
Should do the trick....


Author Comment

ID: 11704239

Our friend I know this, but this is only for Example but I want this value to be String!!!!!!
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 14

Expert Comment

by:Daniel Junges
ID: 11704467
use this

double value = 4.125;
char str[10];
sprintf(str, "%.4f", value);


Accepted Solution

tinchos earned 50 total points
ID: 11704768
try with

      * Converts a double into a string with n decimal characters.
      * @param d The double to convert.
      * @param n The number of digits to the right of the decimal point.
      * @return Thr resulting string.
      std::string doubleToString( double d, int decimals )
            std::stringstream ss;

            ss.precision( decimals );            // set precision
            ss.setf( std::ios::fixed );     // get rid off the scientific notation

            ss << d;
            return ss.str ();

Hope this helps


Expert Comment

ID: 11722004
I like to use the following template function, that converts all streamable types to strings:

template<typename T> string toString(const T& source)
  ostringstream oss;
  oss << source;
  return oss.str();

Then, for floats, you can do:
string s = toString(5.4);

But it would also work for ints, bools, etc.
The opposite conversion is simliar.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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. …
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 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.

828 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