A Way of Converting Float to string

This is a discussion on A Way of Converting Float to string within the C++ Programming forums, part of the General Programming Boards category; Hi, I was wondering if there was a way to copy a float to a string. My current technique (until ...

  1. #1
    Registered User jimboob's Avatar
    Join Date
    Jun 2004
    Posts
    40

    Question A Way of Converting Float to string

    Hi,
    I was wondering if there was a way to copy a float to a string. My current technique (until i figure out a better one) is to load the float into a file from ofstream and then read it back in as a string from ifstream. It's extremely dodgy I know. Any help appreciated.
    Last edited by jimboob; 10-07-2004 at 01:11 AM. Reason: Spelling Errors

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Use a string stream:
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main()
    {
        float f = 2.345f;
        std::ostringstream ostr;
        ostr << f;
        std::string newStr = ostr.str();
        std::cout << newStr;
    }
    If you are using C style strings, then try sprintf or look in the FAQ.
    Last edited by jlou; 10-07-2004 at 11:28 AM. Reason: Changed to ostringstream (thanks swoopy)

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    look up ftoa()

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    ftoa is not standard, but an MS extension.

    If the ostringstream syntax is too long for you, try Boost.LexicalCast.
    http://www.boost.org/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    What does std:: do? I've been trying to look it up in my book, but I can't find anything about it, other than the 'using namespace std;' part.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I explain what namespaces (such as std) are here:
    http://stud3.tuwien.ac.at/~e0226430/...mp_start.xhtml
    Since it's true XHTML, IE can't view it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    What should I use to view it?

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Firefox, Mozilla, Opera...
    Any good browsers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    Quote Originally Posted by CornedBee
    Firefox, Mozilla, Opera...
    Any good browsers.
    Which one of those would you recommend I use?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well, Opera has those banners unless you pay, so I've never even tried it. I use Mozilla, because it comes with a mail client and the two are really well integrated. If you don't care for that, get Firefox.

    www.getfirefox.com
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    I'll do that. Thanks for the help.

  12. #12
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Honestly, try both...
    I find Opera much faster than IE, and it also has a mail client, and I like very much the interface.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > std::istringstream istr;
    One comment, I think it needs to be an ostringstream.

  14. #14
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by swoopy
    > std::istringstream istr;
    One comment, I think it needs to be an ostringstream.
    Thank you... you are right... I updated the original post.

    [EDIT] - Forgot to add this link with a very nice examples of float to string and string to float (or any other type) code:

    http://www.parashift.com/c++-faq-lit...al-issues.html

    38.1 applies to this question, but 38.1-38.3 are all appropriate to the topic.
    Last edited by jlou; 10-07-2004 at 11:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21