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. Question about CString Format

Question about CString Format

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
10 Posts 3 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.
  • K Offline
    K Offline
    KellyR
    wrote on last edited by
    #1

    Hi, so here's my problem: Basically I want to format a CString such that the trailing zeroes are all eliminated. For example, let's say I have a double such as: 610.15100000 And I want to print this to a CString so that I can set the text in an edit control. Right now I do something like this: CString strTemp; double fVal = 610.151; strTemp.Format(TEXT("%lf%), fVal); But in the edit control, it shows up with all the zeroes at the end: 610.15100000000 How do I make it print so that it shaves off the extra zeroes at the end? Thanks!

    KR

    D C 2 Replies Last reply
    0
    • K KellyR

      Hi, so here's my problem: Basically I want to format a CString such that the trailing zeroes are all eliminated. For example, let's say I have a double such as: 610.15100000 And I want to print this to a CString so that I can set the text in an edit control. Right now I do something like this: CString strTemp; double fVal = 610.151; strTemp.Format(TEXT("%lf%), fVal); But in the edit control, it shows up with all the zeroes at the end: 610.15100000000 How do I make it print so that it shaves off the extra zeroes at the end? Thanks!

      KR

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      KellyR wrote:

      How do I make it print so that it shaves off the extra zeroes at the end?

      Assuming you want three places after the decimal, use:

      strTemp.Format(TEXT("%.3f"), fVal);

      If, however, you are wanting to remove all trailing zeros regardless of how many, see the TrimRight() method.

      "Love people and use things, not love things and use people." - Unknown

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      modified on Friday, April 18, 2008 11:58 AM

      C 1 Reply Last reply
      0
      • K KellyR

        Hi, so here's my problem: Basically I want to format a CString such that the trailing zeroes are all eliminated. For example, let's say I have a double such as: 610.15100000 And I want to print this to a CString so that I can set the text in an edit control. Right now I do something like this: CString strTemp; double fVal = 610.151; strTemp.Format(TEXT("%lf%), fVal); But in the edit control, it shows up with all the zeroes at the end: 610.15100000000 How do I make it print so that it shaves off the extra zeroes at the end? Thanks!

        KR

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        You may use strTmp.TrimRight(_T("0")); to the purpose. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        K 1 Reply Last reply
        0
        • D David Crow

          KellyR wrote:

          How do I make it print so that it shaves off the extra zeroes at the end?

          Assuming you want three places after the decimal, use:

          strTemp.Format(TEXT("%.3f"), fVal);

          If, however, you are wanting to remove all trailing zeros regardless of how many, see the TrimRight() method.

          "Love people and use things, not love things and use people." - Unknown

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          modified on Friday, April 18, 2008 11:58 AM

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          The above will fail on fVal = 1.051000 (provided I understood his requirements). [added] Actually it fails also on his input (610.15100000). [/added] :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          D 1 Reply Last reply
          0
          • C CPallini

            The above will fail on fVal = 1.051000 (provided I understood his requirements). [added] Actually it fails also on his input (610.15100000). [/added] :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            CPallini wrote:

            The above will fail on fVal = 1.051000

            How so? strTemp has a value of 1.

            CPallini wrote:

            Actually it fails also on his input (610.15100000).

            It worked fine for me. strTemp has a value of 610.

            "Love people and use things, not love things and use people." - Unknown

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            K C 2 Replies Last reply
            0
            • C CPallini

              You may use strTmp.TrimRight(_T("0")); to the purpose. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              K Offline
              K Offline
              KellyR
              wrote on last edited by
              #6

              strTmp.TrimRight(_T("0")); works as long as I have numbers after the decimal, but if I don't, it leaves me with whole numbers that end with a decimal, i.e: 610.00000 would become: 610. However, strTmp.TrimRight(_T(".0")); seems to work! Thank you much!

              KR

              1 Reply Last reply
              0
              • D David Crow

                CPallini wrote:

                The above will fail on fVal = 1.051000

                How so? strTemp has a value of 1.

                CPallini wrote:

                Actually it fails also on his input (610.15100000).

                It worked fine for me. strTemp has a value of 610.

                "Love people and use things, not love things and use people." - Unknown

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                K Offline
                K Offline
                KellyR
                wrote on last edited by
                #7

                DavidCrow wrote:

                It worked fine for me. strTemp has a value of 610.

                But it needs to be 610.151. Thanks though!

                KR

                D 1 Reply Last reply
                0
                • K KellyR

                  DavidCrow wrote:

                  It worked fine for me. strTemp has a value of 610.

                  But it needs to be 610.151. Thanks though!

                  KR

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  So use .3 instead of .0 then. :rolleyes:

                  "Love people and use things, not love things and use people." - Unknown

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  1 Reply Last reply
                  0
                  • D David Crow

                    CPallini wrote:

                    The above will fail on fVal = 1.051000

                    How so? strTemp has a value of 1.

                    CPallini wrote:

                    Actually it fails also on his input (610.15100000).

                    It worked fine for me. strTemp has a value of 610.

                    "Love people and use things, not love things and use people." - Unknown

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    C Offline
                    C Offline
                    CPallini
                    wrote on last edited by
                    #9

                    I think he wants to remove trailing zeroes in a general way, i.e. that his input was just an example. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                    D 1 Reply Last reply
                    0
                    • C CPallini

                      I think he wants to remove trailing zeroes in a general way, i.e. that his input was just an example. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      CPallini wrote:

                      I think he wants to remove trailing zeroes...

                      Yeah, I read it wrong. I thought he wanted to remove everything after the decimal. Sorry for the confusion.

                      "Love people and use things, not love things and use people." - Unknown

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      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