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. How to pad a string with zeroes

How to pad a string with zeroes

Scheduled Pinned Locked Moved C#
tutorialquestion
14 Posts 5 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.
  • J jkpieters

    Hi, I have the following string "455.56" I want this to format in: 00455.5600. I tried this and was expecting it to work fine: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", "455.66"); This statement results in: "455.66"!! Why doesn't this work as expected? Other solutions? thanks for your time. Regards, Jan

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #2

    Try feeding it a number rather than a string...

    string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", 455.66);

    The format info :00000.0000 is for numeric formats only: MSDN String.Format formats[^] [edit]Should have explained the format is numeric... Oops![/edit]

    You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    J 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      Try feeding it a number rather than a string...

      string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", 455.66);

      The format info :00000.0000 is for numeric formats only: MSDN String.Format formats[^] [edit]Should have explained the format is numeric... Oops![/edit]

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      J Offline
      J Offline
      jkpieters
      wrote on last edited by
      #3

      Thanks for the answer, however I did fed it a number also like this String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", 455.66); But the point is that the data to be formatted is in a string[] and even when I convert the string first to a double or whatever the result is the same, even the conversion is giving funky results: 45566 instead of 455.66!! I am not sure but I must be doing something stupid probably.

      OriginalGriffO 1 Reply Last reply
      0
      • J jkpieters

        Hi, I have the following string "455.56" I want this to format in: 00455.5600. I tried this and was expecting it to work fine: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", "455.66"); This statement results in: "455.66"!! Why doesn't this work as expected? Other solutions? thanks for your time. Regards, Jan

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

        jkpieters wrote:

        Why doesn't this work as expected?

        Because it's a string rather than a number?

        jkpieters wrote:

        Other solutions?

        1. Don't have it as a string (parse it if necessary X| ). 1) Use PadLeft and PadRight. 2) Perhaps use my ApplyFormat[^]. System.Console.WriteLine ( "455.56".ApplyFormat ( "/10,10,0:" ) ) ; But getting the decimal points to line up might be tricky in some cases.
        1 Reply Last reply
        0
        • J jkpieters

          Thanks for the answer, however I did fed it a number also like this String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", 455.66); But the point is that the data to be formatted is in a string[] and even when I convert the string first to a double or whatever the result is the same, even the conversion is giving funky results: 45566 instead of 455.66!! I am not sure but I must be doing something stupid probably.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #5

          To do this while keeping it as a string will be messy:

                  string x = "456.78";
                  string\[\] parts = x.Split('.');
                  string whole = parts\[0\];
                  string decpart = parts\[1\];
                  string y = new string('0', 5 - whole.Length) + whole;
                  string z = decpart + new string('0', 4 - decpart.Length);
                  string padded = y + "." + z;
          

          And that has no error checking! To convert to double and back is probably the best way:

                  string x = "456.78";
                  double d = double.Parse(x, System.Globalization.CultureInfo.InvariantCulture);
                  string padded = string.Format("{0:00000.0000}", d);
          

          But you will have to be absolutely sure that your number strings are all in "nnn.nn" format - remember that some cultures use "nnn,nn" which would bolox you right up! (That may be why your conversion gave funky results...)

          You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          P 1 Reply Last reply
          0
          • J jkpieters

            Hi, I have the following string "455.56" I want this to format in: 00455.5600. I tried this and was expecting it to work fine: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", "455.66"); This statement results in: "455.66"!! Why doesn't this work as expected? Other solutions? thanks for your time. Regards, Jan

            O Offline
            O Offline
            OkkiePepernoot
            wrote on last edited by
            #6

            Not sure if this is what you mean but try this: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", Double.Parse("455.66", CultureInfo.InvariantCulture));

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              To do this while keeping it as a string will be messy:

                      string x = "456.78";
                      string\[\] parts = x.Split('.');
                      string whole = parts\[0\];
                      string decpart = parts\[1\];
                      string y = new string('0', 5 - whole.Length) + whole;
                      string z = decpart + new string('0', 4 - decpart.Length);
                      string padded = y + "." + z;
              

              And that has no error checking! To convert to double and back is probably the best way:

                      string x = "456.78";
                      double d = double.Parse(x, System.Globalization.CultureInfo.InvariantCulture);
                      string padded = string.Format("{0:00000.0000}", d);
              

              But you will have to be absolutely sure that your number strings are all in "nnn.nn" format - remember that some cultures use "nnn,nn" which would bolox you right up! (That may be why your conversion gave funky results...)

              You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

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

              I'd use a Regular Expression to perform the split.

              OriginalGriffO 1 Reply Last reply
              0
              • P PIEBALDconsult

                I'd use a Regular Expression to perform the split.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #8

                I thought of doing it via Linq to SQL and a stored procedure, but decided it was too much typing... :laugh:

                You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                P 1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  I thought of doing it via Linq to SQL and a stored procedure, but decided it was too much typing... :laugh:

                  You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

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

                  I would think your small intestine would leap up your throat and throttle your brain if you tried that.

                  OriginalGriffO 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    I would think your small intestine would leap up your throat and throttle your brain if you tried that.

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #10

                    Yeah, but it'd be worth it - I could post it to "Coding Horrors"

                    You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    • J jkpieters

                      Hi, I have the following string "455.56" I want this to format in: 00455.5600. I tried this and was expecting it to work fine: string strLong = String.Format(CultureInfo.InvariantCulture, "{0:00000.0000}", "455.66"); This statement results in: "455.66"!! Why doesn't this work as expected? Other solutions? thanks for your time. Regards, Jan

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #11

                      I hope you don't get any negative numbers... :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      Prolific encyclopedia fixture proof-reader browser patron addict?
                      We all depend on the beast below.


                      P 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        I hope you don't get any negative numbers... :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        Prolific encyclopedia fixture proof-reader browser patron addict?
                        We all depend on the beast below.


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

                        They're strings, not numbers. :-D

                        L 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          They're strings, not numbers. :-D

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #13

                          even numeric strings could have a negative inclination. :)

                          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                          Prolific encyclopedia fixture proof-reader browser patron addict?
                          We all depend on the beast below.


                          P 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            even numeric strings could have a negative inclination. :)

                            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                            Prolific encyclopedia fixture proof-reader browser patron addict?
                            We all depend on the beast below.


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

                            Luc Pattyn wrote:

                            numeric strings

                            Oxymoron. :-D

                            Luc Pattyn wrote:

                            negative inclination

                            I decline to respond to that.

                            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