double to String in c++ linux

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  
Who is Participating?
tinchosConnect With a Mentor Commented:
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

why don't you use sprintf?
Ok... I would say using sprintf.
Should do the trick....

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

albusaidiAuthor Commented:

Our friend I know this, but this is only for Example but I want this value to be String!!!!!!
Daniel JungesCommented:
use this

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

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.