Solved

double to String in c++ linux

Posted on 2004-08-03
9
927 Views
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 : " ;
         cin>>test;
          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  
0
Comment
Question by:albusaidi
9 Comments
 
LVL 2

Expert Comment

by:sin_
ID: 11703034
why don't you use sprintf?
sprintf(result,"%5lf",test);
0
 

Expert Comment

by:aakash_mandhar
ID: 11703393
Ok... I would say using sprintf.
spritnf(string,"%lf",double_number);
Should do the trick....

Regards
Aakash
0
 

Author Comment

by:albusaidi
ID: 11704239

Our friend I know this, but this is only for Example but I want this value to be String!!!!!!
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 14

Expert Comment

by:Daniel Junges
ID: 11704467
use this

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

junges
0
 
LVL 9

Accepted Solution

by:
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

Tincho
0
 
LVL 4

Expert Comment

by:AssafLavie
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.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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.

815 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

11 Experts available now in Live!

Get 1:1 Help Now