Double to String not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jwmaiden
    New Member
    • Mar 2007
    • 27

    Double to String not working

    I'm trying to convert a double to a string and can't get it to work. I've tried Convert.ToStrin g(...) and a few other permutations (System::Conver t.ToString(...) , etc), but nothing works. For some reason I keep getting back that Convert is not a part of the System namespace. Basically, the compiler can't recognize ToString() as a usable function. Any ideas on what's going wrong?
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    If you are using c++ you can use sprintf()

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      Yeah, do that.
      Last edited by sicarie; Mar 9 '07, 04:38 AM. Reason: Willakawill came up with a much easier answer

      Comment

      • jwmaiden
        New Member
        • Mar 2007
        • 27

        #4
        I've tried sprintf() and my doubles are being rounded. I'll go back and see what's going wrong there.

        Any input on why Convert and ToString isn't working?

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by jwmaiden
          I've tried sprintf() and my doubles are being rounded. I'll go back and see what's going wrong there.

          Any input on why Convert and ToString isn't working?
          if you are using C++ you can functions in <sstream> to convert double, int, etc to a string, e.g.
          Code:
              double pi = 3.14159;
              ostringstream strout(ios_base::out);
              strout << pi << endl;
              cout << "pi as a string = " << strout.str() << endl;
          I have never heard of functions Convert and ToString in C/C++ - do you have a third party API?

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by jwmaiden
            I've tried sprintf() and my doubles are being rounded. I'll go back and see what's going wrong there.
            And are you setting your precision when you use sprintf() (and it's still being rounded)?

            Comment

            • willakawill
              Top Contributor
              • Oct 2006
              • 1646

              #7
              Originally posted by jwmaiden
              I've tried sprintf() and my doubles are being rounded. I'll go back and see what's going wrong there.

              Any input on why Convert and ToString isn't working?
              You are using .net and getting confused with the different languages.

              Comment

              • jwmaiden
                New Member
                • Mar 2007
                • 27

                #8
                Yes, I am. Sorry for any confusion. Should I repost the question in .net?

                Comment

                • willakawill
                  Top Contributor
                  • Oct 2006
                  • 1646

                  #9
                  No. Following your thinking in this thread leads me to the conclusion that you will only confuse yourself doing that. We can get the answer together right here. First can you give us the following info?
                  What compiler are you using?
                  What language are you using?

                  Comment

                  • jwmaiden
                    New Member
                    • Mar 2007
                    • 27

                    #10
                    Ok. I'm using Borland Developer Studio and c++.

                    Comment

                    • willakawill
                      Top Contributor
                      • Oct 2006
                      • 1646

                      #11
                      Great. sprintf() is part of the standard c++ library so it will work with your compiler. As for the precision problem you said you were having, you can control that with the format character. You can read all about sprintf() here.Good luck

                      Comment

                      • jwmaiden
                        New Member
                        • Mar 2007
                        • 27

                        #12
                        Thanks, it worked.

                        Comment

                        • willakawill
                          Top Contributor
                          • Oct 2006
                          • 1646

                          #13
                          Originally posted by jwmaiden
                          Thanks, it worked.
                          You are very welcome

                          Comment

                          Working...