String format in .NET: convert integer to fixed width string?
Another option would be:
i.ToString("d3")
The simplest way to do this is use .NET's built-in functionality for this:
var r = 10;
var p = r.ToString("000");
No need for looping or padding.
Take a look at PadLeft
.
ex:
int i = 40;
string s = i.ToString().PadLeft(3, '0');
s == "040"