views:

51

answers:

1

Is there any simple method to concatenate '0' before an int variable.

Like:

int i = 2;

// produce    
i = someMethod(i);

// output:
i = 02
+4  A: 

If you mean "concatenate", then you can define someMethod() as follows:

string someMethod(int i){
  return string.Format("{0:d2}", i);
}

The "2" in the string format defines the number of characters in the output.

willvv
`return i.ToString("d2");` would work just as well.
Gabe
Gabe is right, you can also use his method.
willvv
To throw my two cents worth: return i.ToString("0#"); will also work
Steve Ellinger