locked
How to add 00000001 before number RRS feed

  • Question

  • I have eight digit number id if the number is one i want to add it like 00000001, if the number 355 it sholud save 00000355 how can i do this process

    New born in developing

    Monday, November 26, 2012 8:03 AM

Answers

All replies

  • If you are going to store the padded number as a string then you could do the following:

    static string CreatePaddedNumberString(int i)
            {
                int paddedNumberLength = 8;
    
                string paddedNumber = i.ToString();
    
                if (paddedNumber.Length > paddedNumberLength)
                {
                    throw new ArgumentException("input number is too big");
                }
                else if (paddedNumber.Length < paddedNumberLength)
                {
                    paddedNumber = paddedNumber.PadLeft(paddedNumberLength, '0');
                }
    
                return paddedNumber;
            }

    If not is this just a case of displaying the integer in the padded format then I think you would be better of just formatting it in the UI.
    Monday, November 26, 2012 8:27 AM
  • Hi,

    In c# you can do like this also

      string val = "76";
                string abc = new string('0', (8 - val.Length));
                val = string.Concat(abc,val);


    Please mark as answer, if you find this post helpful. Thanks Deepak Kalra

    • Proposed as answer by Deepak Kalra Monday, November 26, 2012 8:42 AM
    Monday, November 26, 2012 8:41 AM
  • how should i use it for perticular textbox? or column in SQL.

    i want Pat_no to be saved as 00000235 in sql table 

    protected void btn_Click(object sender, EventArgs e)
            {
                con.Open();
                string s = "Insert into  Pat_sample (Pat_no,Pat_ID_NO,Pat_name)values(@Pat_no,@Pat_ID_NO,@Pat_name)";
                SqlCommand cmd = new SqlCommand(s,con);
                cmd.Parameters.AddWithValue("@Pat_no", TextBox1.Text);
                
                cmd.Parameters.AddWithValue("@Pat_ID_NO", TextBox3.Text);
                cmd.Parameters.AddWithValue("@Pat_name", TextBox2.Text);
                cmd.ExecuteNonQuery();
    
                con.Close();
                btn.Enabled = false;
                }


    New born in developing

    Monday, November 26, 2012 11:44 AM
  • Hi,

    your column type in database table should be varchar . Then solutions proposed by Deepak Kalra and J Chase should solve your problem .

    Monday, November 26, 2012 12:56 PM
  • even simpler:

    "1".PadLeft(8, '0');         // "00000001"
    "355".PadLeft(8, '0');       // "00000355"
    "123456789".PadLeft(8, '0'); // "123456789"
    

    Monday, November 26, 2012 1:01 PM
  • You can use formatting directive like
     
    string.Format ("{0:00000000}",myNumber)
     
    you can also specify different format for negative or null number, f.i:
     
    string.format ("{0:00000000;(00000000)}",myNumber)
     
    to enclose negative number between parenthesis
     
    /LM
    I have eight digit number id if the number is one i want to add it like 00000001, if the number 355 it sholud save 00000355 how can i do this process

    New born in developing


    /LM
    Monday, November 26, 2012 3:10 PM