Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Double to string

Double to string

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++phpperlhelp
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I know this sounds like a stupid question... I need to convert a double to a string to output it with SetWindowText(). I know how to get the string into the double (using atof() ), is there a better way to do it though? Thanks... i'm just getting into C++ and windows programing from Perl and PHP and would appriciate any help. :)

    B J L M 4 Replies Last reply
    0
    • L Lost User

      I know this sounds like a stupid question... I need to convert a double to a string to output it with SetWindowText(). I know how to get the string into the double (using atof() ), is there a better way to do it though? Thanks... i'm just getting into C++ and windows programing from Perl and PHP and would appriciate any help. :)

      B Offline
      B Offline
      Bret Faller
      wrote on last edited by
      #2

      strDouble.Format(_T("%2f"), dDouble); This will work just fine, it will give you a precision of 2. Replace the 2 with however many numbers after the decimal you want it to be precise to. Bret Faller Odyssey Computing, Inc.

      J 2 Replies Last reply
      0
      • L Lost User

        I know this sounds like a stupid question... I need to convert a double to a string to output it with SetWindowText(). I know how to get the string into the double (using atof() ), is there a better way to do it though? Thanks... i'm just getting into C++ and windows programing from Perl and PHP and would appriciate any help. :)

        J Offline
        J Offline
        Jake Palmer
        wrote on last edited by
        #3

        To get a double into a string: double num = 10.0; CString str; str.Format("%d", num); something.SetWindowText(str); The syntax for Format probably looks really ugly if you've never programmed in C, but that %d grabs a double from after the comma and sticks it in there as a string. enjoy :-D Jake

        L B 2 Replies Last reply
        0
        • L Lost User

          I know this sounds like a stupid question... I need to convert a double to a string to output it with SetWindowText(). I know how to get the string into the double (using atof() ), is there a better way to do it though? Thanks... i'm just getting into C++ and windows programing from Perl and PHP and would appriciate any help. :)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          you could eaven if you dont want to use the CString class char buf[32]; double d = 123; wsprintf(buf, "%d", d);

          1 Reply Last reply
          0
          • J Jake Palmer

            To get a double into a string: double num = 10.0; CString str; str.Format("%d", num); something.SetWindowText(str); The syntax for Format probably looks really ugly if you've never programmed in C, but that %d grabs a double from after the comma and sticks it in there as a string. enjoy :-D Jake

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Thanks... I do have some C experience, just not enough to know any of the prinf and formatting stuff.

            1 Reply Last reply
            0
            • J Jake Palmer

              To get a double into a string: double num = 10.0; CString str; str.Format("%d", num); something.SetWindowText(str); The syntax for Format probably looks really ugly if you've never programmed in C, but that %d grabs a double from after the comma and sticks it in there as a string. enjoy :-D Jake

              B Offline
              B Offline
              briefcase
              wrote on last edited by
              #6

              How can you specify the accuracy (places after the decimal place) with str.Format()? Thanks Greg

              1 Reply Last reply
              0
              • L Lost User

                I know this sounds like a stupid question... I need to convert a double to a string to output it with SetWindowText(). I know how to get the string into the double (using atof() ), is there a better way to do it though? Thanks... i'm just getting into C++ and windows programing from Perl and PHP and would appriciate any help. :)

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #7

                The examples so far have been wrong, %d is the format string for ints. Use %lf ("long float") for doubles. --Mike-- http://home.inreach.com/mdunn/ A recent survey reports that 1/4 of all internet users in England surf for porn. The other 3/4 just didn't want to admit it.

                1 Reply Last reply
                0
                • B Bret Faller

                  strDouble.Format(_T("%2f"), dDouble); This will work just fine, it will give you a precision of 2. Replace the 2 with however many numbers after the decimal you want it to be precise to. Bret Faller Odyssey Computing, Inc.

                  J Offline
                  J Offline
                  Jesse Rosalia
                  wrote on last edited by
                  #8

                  Actually I believe you need to use %.2f to get a precision of 2...what you have will produce a string that containts a number with 2 overall digits. -Jesse

                  1 Reply Last reply
                  0
                  • B Bret Faller

                    strDouble.Format(_T("%2f"), dDouble); This will work just fine, it will give you a precision of 2. Replace the 2 with however many numbers after the decimal you want it to be precise to. Bret Faller Odyssey Computing, Inc.

                    J Offline
                    J Offline
                    Jesse Rosalia
                    wrote on last edited by
                    #9

                    Actually I believe you need to use %.2f to get a precision of 2...what you have will produce a string that containts a number with 2 overall digits -Jesse

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups