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. Other Discussions
  3. The Weird and The Wonderful
  4. Padding an ID to six digits

Padding an ID to six digits

Scheduled Pinned Locked Moved The Weird and The Wonderful
ruby
14 Posts 11 Posters 2 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.
  • C Offline
    C Offline
    ClementsDan
    wrote on last edited by
    #1

    I found this little gem in an app I worked on recently.

    switch (strValue.GetLength())
    {
    case 1:
    {
    strValue = "00000" + strValue;
    // do something with strValue
    break;
    }
    case 2:
    {
    strValue = "0000" + strValue;
    // do something with strValue
    break;
    }
    case 3:
    {
    strValue = "000" + strValue;
    // do something with strValue
    break;
    }
    case 4:
    {
    strValue = "00" + strValue;
    // do something with strValue
    break;
    }
    case 5:
    {
    strValue = "0" + strValue;
    // do something with strValue
    break;
    }
    case 6:
    {
    // do something with strValue
    break;
    }
    }

    And yes, the code I replaced with comments is the same for all 6 cases.

    _ S G B P 8 Replies Last reply
    0
    • C ClementsDan

      I found this little gem in an app I worked on recently.

      switch (strValue.GetLength())
      {
      case 1:
      {
      strValue = "00000" + strValue;
      // do something with strValue
      break;
      }
      case 2:
      {
      strValue = "0000" + strValue;
      // do something with strValue
      break;
      }
      case 3:
      {
      strValue = "000" + strValue;
      // do something with strValue
      break;
      }
      case 4:
      {
      strValue = "00" + strValue;
      // do something with strValue
      break;
      }
      case 5:
      {
      strValue = "0" + strValue;
      // do something with strValue
      break;
      }
      case 6:
      {
      // do something with strValue
      break;
      }
      }

      And yes, the code I replaced with comments is the same for all 6 cases.

      _ Offline
      _ Offline
      _Damian S_
      wrote on last edited by
      #2

      Hey, those zeroes are valuable, you can't just go throwing them around willy-nilly!! Sure, it could have been done with a single append then take the right 6 chars, but how many 0's would have been needlessly lost? Consigned to the bit-bucket of history, before their time... Show some mercy, man!! :laugh:

      -------------------------------------------------------- Knowledge is knowing that the tomato is a fruit. Wisdom is not putting it in fruit salad!!

      P 1 Reply Last reply
      0
      • C ClementsDan

        I found this little gem in an app I worked on recently.

        switch (strValue.GetLength())
        {
        case 1:
        {
        strValue = "00000" + strValue;
        // do something with strValue
        break;
        }
        case 2:
        {
        strValue = "0000" + strValue;
        // do something with strValue
        break;
        }
        case 3:
        {
        strValue = "000" + strValue;
        // do something with strValue
        break;
        }
        case 4:
        {
        strValue = "00" + strValue;
        // do something with strValue
        break;
        }
        case 5:
        {
        strValue = "0" + strValue;
        // do something with strValue
        break;
        }
        case 6:
        {
        // do something with strValue
        break;
        }
        }

        And yes, the code I replaced with comments is the same for all 6 cases.

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        Nice. Did you fire up the blame engine (source control) and find the culprit?

        Steve

        L C 2 Replies Last reply
        0
        • C ClementsDan

          I found this little gem in an app I worked on recently.

          switch (strValue.GetLength())
          {
          case 1:
          {
          strValue = "00000" + strValue;
          // do something with strValue
          break;
          }
          case 2:
          {
          strValue = "0000" + strValue;
          // do something with strValue
          break;
          }
          case 3:
          {
          strValue = "000" + strValue;
          // do something with strValue
          break;
          }
          case 4:
          {
          strValue = "00" + strValue;
          // do something with strValue
          break;
          }
          case 5:
          {
          strValue = "0" + strValue;
          // do something with strValue
          break;
          }
          case 6:
          {
          // do something with strValue
          break;
          }
          }

          And yes, the code I replaced with comments is the same for all 6 cases.

          G Offline
          G Offline
          geoffs
          wrote on last edited by
          #4

          Ick - I've seen way too much of this type of thing in code over the last 3 decades. I've come to calling this style of programming "brute force" programming. So, whoever wrote this loses lots of points for elegance (or the lack thereof), but they do get some points in my book for style (use of braces, indentation, etc). No default case however...

          1 Reply Last reply
          0
          • S Stephen Hewitt

            Nice. Did you fire up the blame engine (source control) and find the culprit?

            Steve

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            It does not work that well with outsource contractors! (I have seen similar code from those).

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 alpha 4a out now (29 May 2008)

            1 Reply Last reply
            0
            • C ClementsDan

              I found this little gem in an app I worked on recently.

              switch (strValue.GetLength())
              {
              case 1:
              {
              strValue = "00000" + strValue;
              // do something with strValue
              break;
              }
              case 2:
              {
              strValue = "0000" + strValue;
              // do something with strValue
              break;
              }
              case 3:
              {
              strValue = "000" + strValue;
              // do something with strValue
              break;
              }
              case 4:
              {
              strValue = "00" + strValue;
              // do something with strValue
              break;
              }
              case 5:
              {
              strValue = "0" + strValue;
              // do something with strValue
              break;
              }
              case 6:
              {
              // do something with strValue
              break;
              }
              }

              And yes, the code I replaced with comments is the same for all 6 cases.

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

              This is indeed not good, the switch statement isn't flexible at all. One should master the power off recursion ;)

              CString Pad(CString strValue, int iPadSize)
              {
                if(strValue.GetLength() >= iPadSize)
                {
                  return strValue;
                }
                else
                {
                  strValue = Pad(CString("0") + strValue, iPadSize);
                };
              
                return strValue;
              }
              

              :laugh:

              Learn from the mistakes of others, you may not live long enough to make them all yourself.

              1 Reply Last reply
              0
              • _ _Damian S_

                Hey, those zeroes are valuable, you can't just go throwing them around willy-nilly!! Sure, it could have been done with a single append then take the right 6 chars, but how many 0's would have been needlessly lost? Consigned to the bit-bucket of history, before their time... Show some mercy, man!! :laugh:

                -------------------------------------------------------- Knowledge is knowing that the tomato is a fruit. Wisdom is not putting it in fruit salad!!

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                But... they're insignificant. :-D

                1 Reply Last reply
                0
                • C ClementsDan

                  I found this little gem in an app I worked on recently.

                  switch (strValue.GetLength())
                  {
                  case 1:
                  {
                  strValue = "00000" + strValue;
                  // do something with strValue
                  break;
                  }
                  case 2:
                  {
                  strValue = "0000" + strValue;
                  // do something with strValue
                  break;
                  }
                  case 3:
                  {
                  strValue = "000" + strValue;
                  // do something with strValue
                  break;
                  }
                  case 4:
                  {
                  strValue = "00" + strValue;
                  // do something with strValue
                  break;
                  }
                  case 5:
                  {
                  strValue = "0" + strValue;
                  // do something with strValue
                  break;
                  }
                  case 6:
                  {
                  // do something with strValue
                  break;
                  }
                  }

                  And yes, the code I replaced with comments is the same for all 6 cases.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  He didn't simply append one zero in each case block and then fall through? :confused: Did he ensure the strValue was trimmed? I'm curious what you replaced it with. Of course, in .net, providing the proper format string to int.ToString() will do it in one swell foop.

                  R 1 Reply Last reply
                  0
                  • C ClementsDan

                    I found this little gem in an app I worked on recently.

                    switch (strValue.GetLength())
                    {
                    case 1:
                    {
                    strValue = "00000" + strValue;
                    // do something with strValue
                    break;
                    }
                    case 2:
                    {
                    strValue = "0000" + strValue;
                    // do something with strValue
                    break;
                    }
                    case 3:
                    {
                    strValue = "000" + strValue;
                    // do something with strValue
                    break;
                    }
                    case 4:
                    {
                    strValue = "00" + strValue;
                    // do something with strValue
                    break;
                    }
                    case 5:
                    {
                    strValue = "0" + strValue;
                    // do something with strValue
                    break;
                    }
                    case 6:
                    {
                    // do something with strValue
                    break;
                    }
                    }

                    And yes, the code I replaced with comments is the same for all 6 cases.

                    P Offline
                    P Offline
                    Paul Conrad
                    wrote on last edited by
                    #9

                    Hope they don't work with you anymore.

                    "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      He didn't simply append one zero in each case block and then fall through? :confused: Did he ensure the strValue was trimmed? I'm curious what you replaced it with. Of course, in .net, providing the proper format string to int.ToString() will do it in one swell foop.

                      R Offline
                      R Offline
                      Ri Qen Sin
                      wrote on last edited by
                      #10

                      I believe it would be int.ToString(some_string, "000000");. Of course, I haven't programmed for a while, so I might be wrong.

                      So the creationist says: Everything must have a designer. God designed everything. I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?

                      P 1 Reply Last reply
                      0
                      • R Ri Qen Sin

                        I believe it would be int.ToString(some_string, "000000");. Of course, I haven't programmed for a while, so I might be wrong.

                        So the creationist says: Everything must have a designer. God designed everything. I say: Why is God the only exception? Why not make the "designs" (like man) exceptions and make God a creation of man?

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        Well, kinda, yeah. Assuming the ID begins as an int...

                        int ID = 123 ;
                        ...
                        string strID = ID.ToString ( "000000" ) ;

                        1 Reply Last reply
                        0
                        • C ClementsDan

                          I found this little gem in an app I worked on recently.

                          switch (strValue.GetLength())
                          {
                          case 1:
                          {
                          strValue = "00000" + strValue;
                          // do something with strValue
                          break;
                          }
                          case 2:
                          {
                          strValue = "0000" + strValue;
                          // do something with strValue
                          break;
                          }
                          case 3:
                          {
                          strValue = "000" + strValue;
                          // do something with strValue
                          break;
                          }
                          case 4:
                          {
                          strValue = "00" + strValue;
                          // do something with strValue
                          break;
                          }
                          case 5:
                          {
                          strValue = "0" + strValue;
                          // do something with strValue
                          break;
                          }
                          case 6:
                          {
                          // do something with strValue
                          break;
                          }
                          }

                          And yes, the code I replaced with comments is the same for all 6 cases.

                          S Offline
                          S Offline
                          supercat9
                          wrote on last edited by
                          #12

                          If the 'do something with strValue' is the same in all six cases, it obviously makes sense to pull that out of the case. As for the broader question of whether to append discretely-counted zeros, it's obviously ugly but I believe the code will only generate one new string when it's called. Most other approaches I'm familiar with would generate two new strings.

                          1 Reply Last reply
                          0
                          • S Stephen Hewitt

                            Nice. Did you fire up the blame engine (source control) and find the culprit?

                            Steve

                            C Offline
                            C Offline
                            ClementsDan
                            wrote on last edited by
                            #13

                            That was, in fact, my first reaction. I found the culprit, and he still works here.

                            1 Reply Last reply
                            0
                            • C ClementsDan

                              I found this little gem in an app I worked on recently.

                              switch (strValue.GetLength())
                              {
                              case 1:
                              {
                              strValue = "00000" + strValue;
                              // do something with strValue
                              break;
                              }
                              case 2:
                              {
                              strValue = "0000" + strValue;
                              // do something with strValue
                              break;
                              }
                              case 3:
                              {
                              strValue = "000" + strValue;
                              // do something with strValue
                              break;
                              }
                              case 4:
                              {
                              strValue = "00" + strValue;
                              // do something with strValue
                              break;
                              }
                              case 5:
                              {
                              strValue = "0" + strValue;
                              // do something with strValue
                              break;
                              }
                              case 6:
                              {
                              // do something with strValue
                              break;
                              }
                              }

                              And yes, the code I replaced with comments is the same for all 6 cases.

                              D Offline
                              D Offline
                              dybs
                              wrote on last edited by
                              #14

                              The System.String class has a nice little pair of PadLeft()/PadRight() functions. All it needs is the character to use for padding, and the final length the string needs to be, so that whole switch could be replaced with

                              const int FIXED_LENGTH = 6;
                              str = str->PadLeft('0', FIXED_LENGTH);

                              Dybs

                              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