Results 1 to 9 of 9

Thread: [SOLVED] convert float to string

  1. #1
    Join Date
    Feb 2007
    Beans
    281

    Question [SOLVED] convert float to string

    C++: How do I convert a float into a string?... or a char array... or whatever?

  2. #2
    Join Date
    Feb 2008
    Location
    US
    Beans
    2,782
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: convert float to string

    i think the easiest way is to use sprintf();
    http://www.cplusplus.com/reference/c...o/sprintf.html
    Desktop: Q6600 OC: 343 x 9, 4 GB RAM, 8600 GTS Twinview (22",17"), 1.5 TB RAID 5
    Laptop: Lenovo T61 T7300 @ 2 GHz, 2GB RAM, Nvidia 140M Quadro, 160 GB harddrive
    Remember to mark posts as [SOLVED] when your problem is resolved

  3. #3
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: convert float to string

    Quote Originally Posted by tamoneya View Post
    i think the easiest way is to use sprintf();
    http://www.cplusplus.com/reference/c...o/sprintf.html
    That is, of course, the idiomatic C way of doing it. But the C++ idiom would have you use a stringstream instead.

  4. #4
    Join Date
    Feb 2007
    Beans
    281

    Re: convert float to string

    i think the easiest way is to use sprintf();
    Yea, I stumbled into that a few minutes after posting...
    But the C++ idiom would have you use a stringstream instead.
    I have not heard of this stringstream of which you speak.

  5. #5
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: convert float to string

    The stringstreams are documented here.

    Here's an example of ostringstream:
    PHP Code:
    #include <sstream>     // for ostringstream
    #include <string>
    #include <iostream>

    int main()
    {
      const 
    float fltValue 1234.567890f;
      
    std::ostringstream ostr;

      
    ostr << fltValue;

      
    std::string strValue ostr.str();

      
    std::cout << "fltValue = " << fltValue << std::endl;
      
    std::cout << "strValue = " << strValue << std::endl;

      return 
    0;

    P.S. Does anyone know why the float value gets truncated? I tried using a double; the same thing happens.

  6. #6
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: convert float to string

    A stringstream is like a stream, but it uses a string instead of a file.

    Here's some code containing two versions of a conversion function. One is actually a template that converts "anything" to a string:
    PHP Code:
    #include <iostream>
    #include <string>
    #include <sstream>

    //
    // Use an ostringstream to convert a double to a string...
    //

    std::string double_to_string(double f)
    {
        
    std::ostringstream s;
        
    << f;
        return 
    s.str();
    }

    //
    // By changing "double" to, say, "float" or "int" in the above
    // function, you can create other conversion functions.
    // This ideas leads directly to the following template, which
    // creates a function that converts "anything" to a string.
    //

    template <typename T>
    std::string to_string(const Tvalue)
    {
        
    std::ostringstream s;
        
    << value;
        return 
    s.str();
    }
        

    int main(int argcchar *argv[])
    {
        
    using std::string;
        
    using std::cout;

        
    double d 2.3456;
        
    float f 1.234e-5;
        
    int k 9909;
        
        
    string s0 double_to_string(d);
        
        
    string s1 to_string(d);
        
    string s2 to_string(f);
        
    string s3 to_string(k);
        
        
    cout << "s0 = \"" << s0 << "\"\n";
        
    cout << "s1 = \"" << s1 << "\"\n";
        
    cout << "s2 = \"" << s2 << "\"\n";
        
    cout << "s3 = \"" << s3 << "\"\n";
        
        return 
    0;

    Compile and run:
    Code:
    $ g++ -Wall to_string_example.cpp -o to_string_example
    $ ./to_string_example 
    s0 = "2.3456"
    s1 = "2.3456"
    s2 = "1.234e-05"
    s3 = "9909"
    $

  7. #7
    Join Date
    Nov 2007
    Location
    UK
    Beans
    772
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: convert float to string

    I'm so slow!
    You must have this code ready and waiting for unsuspecting questioners!
    I'm gonna post meh code anyway so I'm not left out.

    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <iomanip>
    
    int main()
    {
    float l_float=3.123456789;
    std::string l_string;
    std::stringstream oss;
    oss << std::setprecision(3) << l_float;
    l_string=oss.str();
    std::cout << "Float as float " << l_float << std::endl;
    std::cout << "Float as string " << l_string << std::endl;
    
    return 0;
    }
    Code:
    pedro@pedro-laptop:~/Documents$ g++ -Wall -pedantic brett.c -o brett.o
    pedro@pedro-laptop:~/Documents$ ./brett.o
    Float as float 3.12346
    Float as string 3.12
    pedro@pedro-laptop:~/Documents$
    You can set the significant figures of the float to the string using the setprecision() method of the iomanip lib.
    I have no idea why the decimal figures are truncated though. Must be a good reason.
    Disclaimer: Yes I usually talk crap

  8. #8
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: convert float to string

    Quote Originally Posted by dwhitney67 View Post
    P.S. Does anyone know why the float value gets truncated? I tried using a double; the same thing happens.
    You can override the default output precision of floating point numbers with the precision method of the ostringstream. E.g.
    Code:
      ostr.precision(10);
      ostr << fltValue;

  9. #9
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: convert float to string

    Quote Originally Posted by dwhitney67 View Post
    P.S. Does anyone know why the float value gets truncated? I tried using a double; the same thing happens.
    It looks like the default precision in the implementation typically bundeled with GNU/Linux is set to such a low value. As you surely know, that can also be changed. http://www.cplusplus.com/reference/i...precision.html

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •