2
char[] charArray = startno.ToCharArray();
//using this arry
//i want to cheque this 

int i=0;
count = 0;
while (chenum [i] != "0")
     {
         count++;
         i++;
     }
     string s = "0";
     string zero = "0";
     for (i = 1; i <= count; i++)
     {
         s = s + zero;
     }

will u help me to correct this code... eg:(00001101) i need to add this no with 1. for that i want to convert this value to int.if i convert to int the no will be(1101)+1 no will be (1102).after adding i want the answer (00001102).

mellamokb
  • 56,094
  • 12
  • 110
  • 136
RAHUL
  • 127
  • 2
  • 8

5 Answers5

6

how many zeros do you want?? You can use string.pad

int count = 1102;
            int NumOfZeros = 10;
            string s = count.ToString().PadLeft(NumOfZeros, '0');

there is also the number formatter.

count.ToString("D10");
Dustin Davis
  • 14,482
  • 13
  • 63
  • 119
2

If you are storing this number as an int (and you should) 1102 annd 00001102 are the same thing. Use string formatting later when you need to output the value with some zeros.

1102.ToString("D8") will give you the string "00001102"

Also, possible duplicates of this question: Pad with leading zeros

Community
  • 1
  • 1
Dyppl
  • 12,161
  • 9
  • 47
  • 68
2
String num = "000001101";
int item = int.Parse(num);
item++;
String output = item.ToString("D8");
John Arlen
  • 6,539
  • 2
  • 33
  • 42
2

You need to use String.Format("{0:00000000}", 1101);, which would be 00001101

Kumar
  • 997
  • 5
  • 8
1

Try int.parseInt(startNo) instead of converting it to a char array.

mellamokb
  • 56,094
  • 12
  • 110
  • 136