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. CString Format method

CString Format method

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 5 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    Hi, I'm trying to control the number of values displayed after the decimal point in a CString. I have a variable which holds the number of values I wish to see after the decimal point. Is there any way of using this variable in the Format method? So rather than having: x.Format(_T("%.5f"), number) I would like the 5 ( or whatever number ) to be replaced by my variable. Thanks.

    C J 2 Replies Last reply
    0
    • A Anonymous

      Hi, I'm trying to control the number of values displayed after the decimal point in a CString. I have a variable which holds the number of values I wish to see after the decimal point. Is there any way of using this variable in the Format method? So rather than having: x.Format(_T("%.5f"), number) I would like the 5 ( or whatever number ) to be replaced by my variable. Thanks.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      CString s; s.Format("%%.%df", 5") s.Format(s, .2346324523); The idea is that the double %% gets turned into a single one, and the single one gets used to put your value in. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

      A 1 Reply Last reply
      0
      • C Christian Graus

        CString s; s.Format("%%.%df", 5") s.Format(s, .2346324523); The idea is that the double %% gets turned into a single one, and the single one gets used to put your value in. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        I keep getting a run-time error. My code is: strx.Format(_T("%%.%f"), signif); strx.Format(strx, m_nMolarity); where signif is the variable that I want to use to specify the number of decimal places after the point and m_nMolarity is the variable which requires formatting. Have I misinterpreted your code? Thanks

        C C 2 Replies Last reply
        0
        • A Anonymous

          I keep getting a run-time error. My code is: strx.Format(_T("%%.%f"), signif); strx.Format(strx, m_nMolarity); where signif is the variable that I want to use to specify the number of decimal places after the point and m_nMolarity is the variable which requires formatting. Have I misinterpreted your code? Thanks

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          You lost the d. "%%.%df", signif formats to %.5f if signif = 5; then format that with m_nMolarity. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

          J 1 Reply Last reply
          0
          • A Anonymous

            I keep getting a run-time error. My code is: strx.Format(_T("%%.%f"), signif); strx.Format(strx, m_nMolarity); where signif is the variable that I want to use to specify the number of decimal places after the point and m_nMolarity is the variable which requires formatting. Have I misinterpreted your code? Thanks

            C Offline
            C Offline
            CoY0te
            wrote on last edited by
            #5

            Yo've missed one little thing: You have: "%%.%f", and You should have "%%.%df" And of course signif must be an integer value; "%%.%f" tries to interpret your signif as floating poin value and it doez not produce the f char for m_nMorality. "%%.%df" will produce "%.f" [ CoY0te ] Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke.

            1 Reply Last reply
            0
            • C Christian Graus

              You lost the d. "%%.%df", signif formats to %.5f if signif = 5; then format that with m_nMolarity. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

              J Offline
              J Offline
              Jon Hulatt
              wrote on last edited by
              #6

              What a cunning stunt! I'm very impressed. STL is a religeon. Enquiries to Reverend Christian Graus

              1 Reply Last reply
              0
              • A Anonymous

                Hi, I'm trying to control the number of values displayed after the decimal point in a CString. I have a variable which holds the number of values I wish to see after the decimal point. Is there any way of using this variable in the Format method? So rather than having: x.Format(_T("%.5f"), number) I would like the 5 ( or whatever number ) to be replaced by my variable. Thanks.

                J Offline
                J Offline
                jbarton
                wrote on last edited by
                #7

                Hi, The easiest way to do this is to use the '*' modifier in your format string. This allows you to specify the number of digits with a variable. The following will format using a variable number of digits: int precision = 5; x.Format( _T("%.*f"), precision, number ); You can also use a * for the width of the field, so the following will set the width and precision: int width = 10; int precision = 3; x.Format( _T("%*.*f"), width, precision, number ); Best regards, John

                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