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. Zero's before number.

Zero's before number.

Scheduled Pinned Locked Moved C#
questiondatabase
8 Posts 7 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
    Satish Developer
    wrote on last edited by
    #1

    Hi, In my database table i have column with data like 00987 78960 89760 08123 009876 when i read the value(00987) into the integer variable, then it contiana value as 987. I need the value in integer variable as same(00987). How can i do this? int k = Convert.ToInt16(dt.rows[i]["colname]);

    G. Satish

    0 C P CPalliniC L 6 Replies Last reply
    0
    • S Satish Developer

      Hi, In my database table i have column with data like 00987 78960 89760 08123 009876 when i read the value(00987) into the integer variable, then it contiana value as 987. I need the value in integer variable as same(00987). How can i do this? int k = Convert.ToInt16(dt.rows[i]["colname]);

      G. Satish

      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      00987 is the same as 987. If you read it into an integer variable, then it will remove leading zeros. If you want to display it as it is in the database, then convert it to a string instead of an integer

      Between the idea And the reality Between the motion And the act Falls the Shadow

      1 Reply Last reply
      0
      • S Satish Developer

        Hi, In my database table i have column with data like 00987 78960 89760 08123 009876 when i read the value(00987) into the integer variable, then it contiana value as 987. I need the value in integer variable as same(00987). How can i do this? int k = Convert.ToInt16(dt.rows[i]["colname]);

        G. Satish

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        Do you know how to count ?

        Christian Graus Driven to the arms of OSX by Vista. Please read this[^] if you don't like the answer I gave to your question. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.

        1 Reply Last reply
        0
        • S Satish Developer

          Hi, In my database table i have column with data like 00987 78960 89760 08123 009876 when i read the value(00987) into the integer variable, then it contiana value as 987. I need the value in integer variable as same(00987). How can i do this? int k = Convert.ToInt16(dt.rows[i]["colname]);

          G. Satish

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          This data is stored as a string in your database. 00987 is exactly the same as 987 - I assume you want it displaying as 00987; in which case you need to use a string to display it.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • S Satish Developer

            Hi, In my database table i have column with data like 00987 78960 89760 08123 009876 when i read the value(00987) into the integer variable, then it contiana value as 987. I need the value in integer variable as same(00987). How can i do this? int k = Convert.ToInt16(dt.rows[i]["colname]);

            G. Satish

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Satish - Developer wrote:

            when i read the value(00987) into the integer variable, then it contiana value as 987. I need the value in integer variable as same(00987)

            You have to do nothing (or just review your basic math course material), since 000987 = 987 holds for a integer. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • S Satish Developer

              Hi, In my database table i have column with data like 00987 78960 89760 08123 009876 when i read the value(00987) into the integer variable, then it contiana value as 987. I need the value in integer variable as same(00987). How can i do this? int k = Convert.ToInt16(dt.rows[i]["colname]);

              G. Satish

              L Offline
              L Offline
              Lutoslaw
              wrote on last edited by
              #6

              I suppose that what you really wanted to ask is: how to read a data with leading zeros and don't loose these zeros. You have two possibilities: - Store the number asa string in your application and parse it each time you need it for calculations. - Create a structure which would store a number of zeros (I would do it this way).

              internal struct IntWithZeros
              {
              private int zerosCount, value;

              public static implicit operator int(IntWithZeros x)
              {
              	return x.value;
              }
              
              // overload other required operators to preserve zerosCount
              
              public static IntWithZeros Parse(string data)
              {
              	IntWithZeros ret;
              	ret.zerosCount = 0;
              	while (data.Length > ret.zerosCount && data\[ret.zerosCount\] == '0') {
              		ret.zerosCount++;
              	}
              	ret.value = int.Parse(data);
              	return ret;
              }
              public override string ToString()
              {
              	string s = value.ToString();
              	return s.PadLeft(s.Length + zerosCount, '0');
              }
              

              }

              Greetings - Jacek Gajek

              1 Reply Last reply
              0
              • S Satish Developer

                Hi, In my database table i have column with data like 00987 78960 89760 08123 009876 when i read the value(00987) into the integer variable, then it contiana value as 987. I need the value in integer variable as same(00987). How can i do this? int k = Convert.ToInt16(dt.rows[i]["colname]);

                G. Satish

                S Offline
                S Offline
                Searril
                wrote on last edited by
                #7

                private void btnUseLeadingZero_Click(object sender, EventArgs e)
                {
                int MyNumber = 987;
                string MyNumberWithLeadingZero = MyNumber.ToString("00000");
                MessageBox.Show(MyNumberWithLeadingZero);
                //displays 00987
                }

                L 1 Reply Last reply
                0
                • S Searril

                  private void btnUseLeadingZero_Click(object sender, EventArgs e)
                  {
                  int MyNumber = 987;
                  string MyNumberWithLeadingZero = MyNumber.ToString("00000");
                  MessageBox.Show(MyNumberWithLeadingZero);
                  //displays 00987
                  }

                  L Offline
                  L Offline
                  Lutoslaw
                  wrote on last edited by
                  #8

                  Simplier and better. 5 from me.

                  Greetings - Jacek Gajek

                  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