How can I convert a number into text and output it to the console using C ++? For example:

float x = 0.05f; char *str; //str = x 

    7 answers 7

    Option 1.

     #include <stdio.h> printf("Number is %f\n", x); 

    Option 2.

     #include <iostream> std::cout << "Number is " << std::scientific << x << std::endl; 
    • This is all true, but the fact of the matter is that in str you need to write x and display it ... - STERLIN
    • one
      As an option - use sprintf (). This is what char * str will write. - ivkremer

    Here is to use C to convert C ++ methods like atoi, sprintf, etc. may be fraught with consequences. Here is a purely C ++ option based on streams, moreover a universal one. You can convert at least integer types, even floating point.

    `

     #include <string> #include <sstream> template <typename T> std::string toString(T val) { std::ostringstream oss; oss<< val; return oss.str(); } template<typename T> T fromString(const std::string& s) { std::istringstream iss(s); T res; iss >> res; return res; } 

    Used as follows

     std::string str; int iVal; float fVal; str = toString(iVal); str = tiString(fVal); iVal = fromString<int>(str); fVal = fromString<float>(str); 

    I took an example from here. Algorithms for converting a string to a number and back (cyberguru)

    • one
      Only in difference from formatting in sprintf () it is not at all clear what the final view will be. - avp
    • why is it unclear? toString () is the final string view. fromString <EndType> () - the end type is specified in angle brackets <>, in this case the end type will be "End Type". - Faceless
    • 2
      You did not understand. I meant what characters (decimal point, plus / minus signs, etc.) and how (placement in the output field) will be displayed in a string. The sprintf () formats have quite rich features, and they are visible 'in place'. How to implement something similar with your method? - avp
    • one
      I would call this code bad. I personally do not understand error handling. Well, templates are cool, but suppose we don't have an integer in the input line, but abracadabra. What will happen? I have already solved this problem here on HashCode. The code was essentially ugly. To some extent, printfs in use are simpler and clearer than this patterned magic. PS claim not to toString, but to fromString. With toString everything is fine. - gecube

    solution in c ++ 11
    http://www.cplusplus.com/reference/string/to_string/

     #include <string> int x = 10; std::string y = std::to_string(x); 

      Use the sprintf function from stdio.h:

       float x = 0.05f; char str[20]; sprintf(str, "%f", x);//или любой другой формат, как это делается в printf() std::cout << str << std::endl; 
         strstream s; float x = 0.05f; s << x; //преобразование в строку 

        upd: you can still use stringstream

        • strstream deprecated; strstream should be used stringstream . - Abyx pm
         #include "stdafx.h" #include <iostream> #include <sstream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { float a = 33.5; int b = 50; char str[50]; _TOSTRING(str, "%f", a); cout << str << endl; _TOSTRING(str, "%d", b); cout << b; return 0; } 
        • useless without stdafx.h content - Abyx
         string flts (long x) { string s; char c; while(x) { c=(x%10)+'0'; s=c+s; x=x/10; } return s; } 
        • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky