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. Convert String to int zero is missing....

Convert String to int zero is missing....

Scheduled Pinned Locked Moved C#
tutorial
13 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.
  • S Offline
    S Offline
    spalanivel
    wrote on last edited by
    #1

    Hi, Seem to be very simple but it's not working with following conversion methods. Trying to convert from string to integer 0's missing in the Output. string strInput = "06461"; int iResult = Convert.ToInt32(strInput) (or) int iResult = int.Parse(strInput); Output : 6461 (note 0's missing) How to get the zero also in the output as 06461. Reg, Subbu

    D OriginalGriffO L 3 Replies Last reply
    0
    • S spalanivel

      Hi, Seem to be very simple but it's not working with following conversion methods. Trying to convert from string to integer 0's missing in the Output. string strInput = "06461"; int iResult = Convert.ToInt32(strInput) (or) int iResult = int.Parse(strInput); Output : 6461 (note 0's missing) How to get the zero also in the output as 06461. Reg, Subbu

      D Offline
      D Offline
      ddecoy
      wrote on last edited by
      #2

      That is normal: an Int of "06461" = 6461 (you don't put 06461 in a calculator, do you ? :^) ) Keep the number as a string to show the leading zero's.

      Learning without thought is labor lost; thought without learning is perilous. (Confucius)

      S 1 Reply Last reply
      0
      • S spalanivel

        Hi, Seem to be very simple but it's not working with following conversion methods. Trying to convert from string to integer 0's missing in the Output. string strInput = "06461"; int iResult = Convert.ToInt32(strInput) (or) int iResult = int.Parse(strInput); Output : 6461 (note 0's missing) How to get the zero also in the output as 06461. Reg, Subbu

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

        Use an explicit format string when you print the int:

        int i = 6461;
        Console.Writeline(i);
        Console.WriteLine(i.ToString("D5"));

        will print:

        6461
        06461

        All those who believe in psycho kinesis, raise my hand.

        "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

        S R 2 Replies Last reply
        0
        • D ddecoy

          That is normal: an Int of "06461" = 6461 (you don't put 06461 in a calculator, do you ? :^) ) Keep the number as a string to show the leading zero's.

          Learning without thought is labor lost; thought without learning is perilous. (Confucius)

          S Offline
          S Offline
          spalanivel
          wrote on last edited by
          #4

          Thanx for your reply. How to keep that as a string and expand it. Example: string strInput = AD01234-AD01237; This need to be Expand like AD01234,AD01235,AD01236,AD01237. - Spliting the string in to 2 with -(hypen) - From 1st string process from the last and get only numbers and vice versa. - finally by using the For loop process from starting Number to end will be expanded. But in this case need to convert to int then only it can be possible. how to achieve this? Reg, subbu

          D 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Use an explicit format string when you print the int:

            int i = 6461;
            Console.Writeline(i);
            Console.WriteLine(i.ToString("D5"));

            will print:

            6461
            06461

            All those who believe in psycho kinesis, raise my hand.

            S Offline
            S Offline
            spalanivel
            wrote on last edited by
            #5

            Thanks for your quick response. But one question how to get the final value as a integer rather than string. Any Other Option? int iResult = 06461; /// Output. REg, Sbubbu

            OriginalGriffO R 2 Replies Last reply
            0
            • S spalanivel

              Thanks for your quick response. But one question how to get the final value as a integer rather than string. Any Other Option? int iResult = 06461; /// Output. REg, Sbubbu

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

              Oh boy. :sigh: An integer is a number. Like any number, it can have an infinite number of zeros in front of it. Take your house, or flat number - let us say it is six, and the address is Lemon Street. You live at 6 Lemon street. You also could say "I live at 0000006 Lemon Street, and your letters would still arive - but the postman might think you are a little wierd. You can happily tell Visual studio:

              int i = 000000000006461;

              or

              int i = 6461;

              or

              int i = 0006461;

              etc., etc. They all mean the same. A string representation of a number is more like a telephone number: "0123 456789" is a different number to "123 456789" because the exchange will not see it as having an area code, as the leading zero is missing. But, you cannot add two telephone numbers together!

              All those who believe in psycho kinesis, raise my hand.

              "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

              D 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Oh boy. :sigh: An integer is a number. Like any number, it can have an infinite number of zeros in front of it. Take your house, or flat number - let us say it is six, and the address is Lemon Street. You live at 6 Lemon street. You also could say "I live at 0000006 Lemon Street, and your letters would still arive - but the postman might think you are a little wierd. You can happily tell Visual studio:

                int i = 000000000006461;

                or

                int i = 6461;

                or

                int i = 0006461;

                etc., etc. They all mean the same. A string representation of a number is more like a telephone number: "0123 456789" is a different number to "123 456789" because the exchange will not see it as having an area code, as the leading zero is missing. But, you cannot add two telephone numbers together!

                All those who believe in psycho kinesis, raise my hand.

                D Offline
                D Offline
                dan sh
                wrote on last edited by
                #7

                This is not mathematics forum. ;P

                50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                L 1 Reply Last reply
                0
                • D dan sh

                  This is not mathematics forum. ;P

                  50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

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

                  we no longer have a math forum, so math goes everywhere. ;P

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


                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                  D 1 Reply Last reply
                  0
                  • S spalanivel

                    Hi, Seem to be very simple but it's not working with following conversion methods. Trying to convert from string to integer 0's missing in the Output. string strInput = "06461"; int iResult = Convert.ToInt32(strInput) (or) int iResult = int.Parse(strInput); Output : 6461 (note 0's missing) How to get the zero also in the output as 06461. Reg, Subbu

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

                    two comments: 1.

                    spalanivel wrote:

                    Output : 6461 (note 0's missing)

                    yes, sure. and how many zeroes are missing? 1? one hundred? 2. you could (I don't say "should") do what you want with strings and characters, i.e. come up with a method that "increments a string". This is how it would go: a. copy the original string; b. look for the rightmost digit that you haven't processed yet; c. add one to it i.e. replace '0' by '1', or '1' by '2', etc d. if in step c you replaced '9' by '0' then goto b e. done with possible refinement: if b fails, prefix a '1' :)

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


                    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      we no longer have a math forum, so math goes everywhere. ;P

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


                      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                      D Offline
                      D Offline
                      dan sh
                      wrote on last edited by
                      #10

                      Luc Pattyn wrote:

                      math goes is everywhere

                      FTFY :-D

                      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

                      1 Reply Last reply
                      0
                      • S spalanivel

                        Thanx for your reply. How to keep that as a string and expand it. Example: string strInput = AD01234-AD01237; This need to be Expand like AD01234,AD01235,AD01236,AD01237. - Spliting the string in to 2 with -(hypen) - From 1st string process from the last and get only numbers and vice versa. - finally by using the For loop process from starting Number to end will be expanded. But in this case need to convert to int then only it can be possible. how to achieve this? Reg, subbu

                        D Offline
                        D Offline
                        ddecoy
                        wrote on last edited by
                        #11

                        You can split a string like this:

                        string[] ArrayOfStrings;
                        string s = "AD0001-AD0002-AD0003";
                        ArrayOfStrings = s.Split('-');

                        For the rest I not sure what u mean...

                        Learning without thought is labor lost; thought without learning is perilous. (Confucius)

                        1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          Use an explicit format string when you print the int:

                          int i = 6461;
                          Console.Writeline(i);
                          Console.WriteLine(i.ToString("D5"));

                          will print:

                          6461
                          06461

                          All those who believe in psycho kinesis, raise my hand.

                          R Offline
                          R Offline
                          realJSOP
                          wrote on last edited by
                          #12

                          Or

                          string.format("{0:00000}", value);

                          .45 ACP - because shooting twice is just silly
                          -----
                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                          -----
                          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                          1 Reply Last reply
                          0
                          • S spalanivel

                            Thanks for your quick response. But one question how to get the final value as a integer rather than string. Any Other Option? int iResult = 06461; /// Output. REg, Sbubbu

                            R Offline
                            R Offline
                            realJSOP
                            wrote on last edited by
                            #13

                            You can't. It's impossible.

                            .45 ACP - because shooting twice is just silly
                            -----
                            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                            -----
                            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                            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