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#
  4. Add five zéros on the left of int

Add five zéros on the left of int

Scheduled Pinned Locked Moved C#
question
20 Posts 8 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.
  • T Tony Pottier

    Brilliant =)

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #9

    :omg: Not the most elegant and simple, obvious. It's just a fun question that has lots of possible solutions. Some solutions even make code-obfuscation irrelevant :laugh:

    I are troll :)

    G 1 Reply Last reply
    0
    • G Guffa

      There are many, many ways... What you want to do according to your question: "00000" + number.ToString() or number.ToString("'00000'0") or string.Format("'00000'{0}", number) or new String('0', 5) + number.ToString() or number.ToString().Insert(0, "00000") What you want to do according to your examples: String.Format("{0:0000000000}", number) or number.ToString("0000000000") or number.ToString().PadLeft(10, '0')

      Despite everything, the person most likely to be fooling you next is yourself.

      R Offline
      R Offline
      Rutvik Dave
      wrote on last edited by
      #10

      wow... I just thought 2 from these.

      1 Reply Last reply
      0
      • L Lost User

        Add the number "1000000000" to the int, that would give you this; 1------------->10000000001 345----------->10000000345 65576--------->10000065576 Now, convert them to a string, and loose the first character. That would give you these strings; 10000000001------------->0000000001 10000000345------------->0000000345 10000065576------------->0000065576 Enjoy :)

        I are troll :)

        R Offline
        R Offline
        Rutvik Dave
        wrote on last edited by
        #11

        there are too many ways of doing same thing... :-D cool. are you an assembley language programmer before ? ;P

        G L 2 Replies Last reply
        0
        • L Lost User

          :omg: Not the most elegant and simple, obvious. It's just a fun question that has lots of possible solutions. Some solutions even make code-obfuscation irrelevant :laugh:

          I are troll :)

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #12

          There sure are some interresting solutions. Here's an almost completely useless way of doing it: String.Join(null,number.ToString().ToCharArray().Reverse().Select(c=>c.ToString()).Concat(new int[10].Select(i=>i.ToString())).Take(10).Reverse().ToArray()) ;)

          Despite everything, the person most likely to be fooling you next is yourself.

          L 1 Reply Last reply
          0
          • R Rutvik Dave

            there are too many ways of doing same thing... :-D cool. are you an assembley language programmer before ? ;P

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #13

            Then it would probably have been a more hardcore solution: char[] c = new char[10]; for (int i = 9; i >= 0; number /= 10) c[i--] = (char)('0' + number % 10); string result = new String(c); ;)

            Despite everything, the person most likely to be fooling you next is yourself.

            R 1 Reply Last reply
            0
            • A abbd

              Hello, I would add five zéros on the left of int, like this : 1------------->0000000001 345----------->0000000345 65576--------->0000065576 How i can make this? thank you verry mutch.

              A Offline
              A Offline
              alantu
              wrote on last edited by
              #14

              try this: string.format("{0:d10}",345);

              1 Reply Last reply
              0
              • G Guffa

                There sure are some interresting solutions. Here's an almost completely useless way of doing it: String.Join(null,number.ToString().ToCharArray().Reverse().Select(c=>c.ToString()).Concat(new int[10].Select(i=>i.ToString())).Take(10).Reverse().ToArray()) ;)

                Despite everything, the person most likely to be fooling you next is yourself.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #15

                LINQ! :-D I haven't done much with Linq yet, but I'll take the time for it this weekend. It's turning up at more and more places, and most people agree that it's a good thing. ..and that would be a better idea than to write a recursive method to padd zeroes, wouldn't it? :laugh:

                I are troll :)

                1 Reply Last reply
                0
                • R Rutvik Dave

                  there are too many ways of doing same thing... :-D cool. are you an assembley language programmer before ? ;P

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #16

                  Noes, I learned it in "Amos Basic", using GWBasic examples :-\ It's been over fifteen years, and there are still days that I'm having trouble with even the most basic statement :laugh:

                  I are troll :)

                  R 1 Reply Last reply
                  0
                  • G Guffa

                    Then it would probably have been a more hardcore solution: char[] c = new char[10]; for (int i = 9; i >= 0; number /= 10) c[i--] = (char)('0' + number % 10); string result = new String(c); ;)

                    Despite everything, the person most likely to be fooling you next is yourself.

                    R Offline
                    R Offline
                    Rutvik Dave
                    wrote on last edited by
                    #17

                    OK. I give up... :-D Hey you forgot

                    asm
                    {

                    }

                    ;P

                    G 1 Reply Last reply
                    0
                    • L Lost User

                      Noes, I learned it in "Amos Basic", using GWBasic examples :-\ It's been over fifteen years, and there are still days that I'm having trouble with even the most basic statement :laugh:

                      I are troll :)

                      R Offline
                      R Offline
                      Rutvik Dave
                      wrote on last edited by
                      #18

                      Eddy Vluggen wrote:

                      It's been over fifteen years

                      wow long time huh... :) I have started with QBasic. But I still remember those college days when in exams they ask some silly things like. *) write a function to swap 2 variables without using 3rd one or references. *) draw a pascal triangle without using array (recurrsion : damn thing) X|

                      L 1 Reply Last reply
                      0
                      • R Rutvik Dave

                        Eddy Vluggen wrote:

                        It's been over fifteen years

                        wow long time huh... :) I have started with QBasic. But I still remember those college days when in exams they ask some silly things like. *) write a function to swap 2 variables without using 3rd one or references. *) draw a pascal triangle without using array (recurrsion : damn thing) X|

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #19

                        Things haven't changed much, my roommate is in college and learning C# using the book "Head First C#". It's got some basic examples as to be expected, and they're gonna build two games during class. One arcade-type, another rpg-type. It looks better when you see the graphics, but is just as much fun as doing a Snakes-game in basic :-D ..and yeah, I do miss those logical puzzles from school sometimes. I don't miss being sent out of class though :suss:

                        I are troll :)

                        1 Reply Last reply
                        0
                        • R Rutvik Dave

                          OK. I give up... :-D Hey you forgot

                          asm
                          {

                          }

                          ;P

                          G Offline
                          G Offline
                          Guffa
                          wrote on last edited by
                          #20

                          No, that wasn't assembly code. If inline asm was supported, it might look more like this:

                          string result;
                          unsafe {
                          char* p = stackalloc char[10];
                          asm {
                          lea esi, number
                          mov eax, [si]
                          lea edi, p
                          add edi, 014
                          std
                          mov cx, 0a
                          .digit
                          xor edx, edx
                          div dword 0a
                          xchg eax, edx
                          add ax, 030
                          stosw
                          xchg eax, edx
                          loop digit
                          }
                          result = new String(c);
                          }

                          :)

                          Despite everything, the person most likely to be fooling you next is yourself.

                          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