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. How to zero-pad a number?

How to zero-pad a number?

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorialquestion
20 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.
  • Richard DeemingR Richard Deeming

    PIEBALDconsult wrote:

    It can't be C#

    Really? ;P

    http://msdn.microsoft.com/en-GB/library/k1a63xkz.aspx[^]

    When one or both operands are of type string, + concatenates the string representations of the operands.


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

    Richard Deeming wrote:

    When one or both operands are of type string

    Yes, and are they?

    Richard DeemingR 1 Reply Last reply
    0
    • P PIEBALDconsult

      Richard Deeming wrote:

      When one or both operands are of type string

      Yes, and are they?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #7

      PIEBALDconsult wrote:

      Yes, and are they?

      Yes, the first operand is a string. It's the result of the ternary operator:

      fileCount <= 999
      ? fileCount <= 99
      ? fileCount <= 9
      ? "000"
      : "00"
      : "0"
      : ""


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      P 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        PIEBALDconsult wrote:

        Yes, and are they?

        Yes, the first operand is a string. It's the result of the ternary operator:

        fileCount <= 999
        ? fileCount <= 99
        ? fileCount <= 9
        ? "000"
        : "00"
        : "0"
        : ""


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

        And the second?

        Richard DeemingR 1 Reply Last reply
        0
        • P PIEBALDconsult

          It's so good to know that the hard work of the developers of String.PadLeft[^] and Int32.ToString[^] is providing so much joy to so many, other than you. By the way, what language is that? It can't be C#, or did you also override the + operator? :omg:

          S Offline
          S Offline
          SortaCore
          wrote on last edited by
          #9

          I think I wrote it in one of those points in the week where I just couldn't think. X| Methinks I'll replace it tomorrow.

          1 Reply Last reply
          0
          • P PIEBALDconsult

            And the second?

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #10

            PIEBALDconsult wrote:

            And the second?

            Doesn't matter. The documentation clearly states, "When one or both operands are of type string...", so the type of the other operand is irrelevant.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            B P 2 Replies Last reply
            0
            • Richard DeemingR Richard Deeming

              PIEBALDconsult wrote:

              And the second?

              Doesn't matter. The documentation clearly states, "When one or both operands are of type string...", so the type of the other operand is irrelevant.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              B Offline
              B Offline
              Big Daddy Farang
              wrote on last edited by
              #11

              I missed that the first time also. :)

              BDF The internet makes dumb people dumber and clever people cleverer. -- PaulowniaK

              1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                PIEBALDconsult wrote:

                And the second?

                Doesn't matter. The documentation clearly states, "When one or both operands are of type string...", so the type of the other operand is irrelevant.


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

                Where do you see that in the documentation? I don't even see + in the documentation. And what idiot thought that was a good idea? :wtf: Time to find a new language; this one's becoming VB. X| Edit: I finally found it; it's not documented with String; what a POS. :thumbsdown: I'm fairly sure that no self-respecting C# professional uses it that way.

                Richard DeemingR S 3 Replies Last reply
                0
                • P PIEBALDconsult

                  Where do you see that in the documentation? I don't even see + in the documentation. And what idiot thought that was a good idea? :wtf: Time to find a new language; this one's becoming VB. X| Edit: I finally found it; it's not documented with String; what a POS. :thumbsdown: I'm fairly sure that no self-respecting C# professional uses it that way.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #13

                  It's the link that I posted in my previous message: http://msdn.microsoft.com/en-GB/library/k1a63xkz.aspx[^]


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Where do you see that in the documentation? I don't even see + in the documentation. And what idiot thought that was a good idea? :wtf: Time to find a new language; this one's becoming VB. X| Edit: I finally found it; it's not documented with String; what a POS. :thumbsdown: I'm fairly sure that no self-respecting C# professional uses it that way.

                    S Offline
                    S Offline
                    SortaCore
                    wrote on last edited by
                    #14

                    PIEBALDconsult wrote:

                    I'm fairly sure that no self-respecting C# professional uses it that way.

                    Heh, I go for the shorter version. If it doesn't make sense with a mishmash of ints/strings I use brackets or ToString(). Like for:

                    int i = 124, j = 253;
                    string s = "JD" + i + j; // Could be two things and no one wants to look up operator precedence, so instead...
                    string t = "JD" + (i + j);

                    But yeah, usually I don't bother. Although, theoretically, I could now do

                    string s = 0; // won't initialise
                    string t = "" + 0; // will work fine O.o

                    So, let's get back to self-respecting... :rolleyes:

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Where do you see that in the documentation? I don't even see + in the documentation. And what idiot thought that was a good idea? :wtf: Time to find a new language; this one's becoming VB. X| Edit: I finally found it; it's not documented with String; what a POS. :thumbsdown: I'm fairly sure that no self-respecting C# professional uses it that way.

                      S Offline
                      S Offline
                      SortaCore
                      wrote on last edited by
                      #15

                      PIEBALDconsult wrote:

                      I'm fairly sure that no self-respecting C# professional uses it that way.

                      Heh, I go for the shorter version. If it doesn't make sense with a mishmash of ints/strings I use brackets or ToString() to clarify.

                      int i = 124, j = 253;
                      string s = "JD" + i + j; // Could be two things and no one wants to look up operator precedence, so instead...
                      string t = "JD" + (i + j);

                      But yeah, usually I don't bother. Although, theoretically, I could now do

                      string s = 0; // won't initialise
                      string t = "" + 0; // will work fine O.o

                      So, let's get back to self-respecting... :rolleyes:

                      P 1 Reply Last reply
                      0
                      • S SortaCore

                        PIEBALDconsult wrote:

                        I'm fairly sure that no self-respecting C# professional uses it that way.

                        Heh, I go for the shorter version. If it doesn't make sense with a mishmash of ints/strings I use brackets or ToString() to clarify.

                        int i = 124, j = 253;
                        string s = "JD" + i + j; // Could be two things and no one wants to look up operator precedence, so instead...
                        string t = "JD" + (i + j);

                        But yeah, usually I don't bother. Although, theoretically, I could now do

                        string s = 0; // won't initialise
                        string t = "" + 0; // will work fine O.o

                        So, let's get back to self-respecting... :rolleyes:

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

                        string t = System.String.Format ( "JD{0}" , i + j ) ;

                        Richard DeemingR 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          string t = System.String.Format ( "JD{0}" , i + j ) ;

                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #17

                          Make that:

                          string t = string.Format(CultureInfo.InvariantCulture, "JD{0:D}", i + j);

                          You typically don't want culture-specific formatting or group separators in the result.


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          P S 2 Replies Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            Make that:

                            string t = string.Format(CultureInfo.InvariantCulture, "JD{0:D}", i + j);

                            You typically don't want culture-specific formatting or group separators in the result.


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

                            I've never had that problem.

                            1 Reply Last reply
                            0
                            • Richard DeemingR Richard Deeming

                              Make that:

                              string t = string.Format(CultureInfo.InvariantCulture, "JD{0:D}", i + j);

                              You typically don't want culture-specific formatting or group separators in the result.


                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              S Offline
                              S Offline
                              SortaCore
                              wrote on last edited by
                              #19

                              And of course don't forget to set the string to "" at the end, else if the garbage collector is a little slow you might run out of memory! Better yet, just keep re-using global strings.

                              1 Reply Last reply
                              0
                              • S SortaCore

                                A padded number in a file name!

                                // Set to padded number
                                int fileCount = Directory.GetFiles("[censored]", "*.csv", SearchOption.AllDirectories).Length + 1;
                                string fileCountStr = (fileCount <= 999 ? fileCount <= 99 ? fileCount <= 9 ? "000" : "00" : "0" : "") + fileCount;

                                I think this should win an award for readability in a triply-nested ternary condition :rolleyes:.

                                D Offline
                                D Offline
                                Dennis_E
                                wrote on last edited by
                                #20

                                Wonderful code! When I was new to programming I used to do something like this too. Then I figured out a trick: just add 1.000.000.000 to the number and take the last 4 characters. Now I just use PadLeft, PadRight or Format

                                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