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 and numbers

CString format and numbers

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

    Is there a straight forward way to make a number like 123 and turn it into a string with left padded zeros. Example "123" into 0000123 A way that does not require a silly loop like this. CString strSecId; strSecId.Format("%d", m_iSectionId); while(strSecId.GetLength() < 7) strSecId = "0" + strSecId; //left pad with zeros thanks

    A B D 3 Replies Last reply
    0
    • M mcgahanfl

      Is there a straight forward way to make a number like 123 and turn it into a string with left padded zeros. Example "123" into 0000123 A way that does not require a silly loop like this. CString strSecId; strSecId.Format("%d", m_iSectionId); while(strSecId.GetLength() < 7) strSecId = "0" + strSecId; //left pad with zeros thanks

      A Offline
      A Offline
      Alexander M
      wrote on last edited by
      #2

      Try "%.7d". Don't try it, just do it! ;-)

      1 Reply Last reply
      0
      • M mcgahanfl

        Is there a straight forward way to make a number like 123 and turn it into a string with left padded zeros. Example "123" into 0000123 A way that does not require a silly loop like this. CString strSecId; strSecId.Format("%d", m_iSectionId); while(strSecId.GetLength() < 7) strSecId = "0" + strSecId; //left pad with zeros thanks

        B Offline
        B Offline
        bugDanny
        wrote on last edited by
        #3

        Your code looks great until that last line. Try this: strSecId.Insert(0, "0"); The first argument is the 0-based index of where you want to insert this string (we want it at the fron, hence the 0) and the second argument is the string to insert (a 0!) Hope this helps! Danny

        1 Reply Last reply
        0
        • M mcgahanfl

          Is there a straight forward way to make a number like 123 and turn it into a string with left padded zeros. Example "123" into 0000123 A way that does not require a silly loop like this. CString strSecId; strSecId.Format("%d", m_iSectionId); while(strSecId.GetLength() < 7) strSecId = "0" + strSecId; //left pad with zeros thanks

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

          Use:

          strSecId.Format("%07d", m_iSectionId);

          M 1 Reply Last reply
          0
          • D David Crow

            Use:

            strSecId.Format("%07d", m_iSectionId);

            M Offline
            M Offline
            mcgahanfl
            wrote on last edited by
            #5

            Don't I feel silly. Thank you.

            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