27

I have an int in .NET/C# that I want to convert to a specifically formatted string.

If the value is 1, I want the string to be "001".

10 = "010".

116 = "116".

etc...

I'm looking around at string formatting, but so far no success. I also won't have values over 999.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pearcewg
  • 9,545
  • 21
  • 79
  • 125

7 Answers7

63

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.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
26

Take a look at PadLeft.

ex:

int i = 40;

string s = i.ToString().PadLeft(3, '0'); 

s == "040"

svick
  • 236,525
  • 50
  • 385
  • 514
ingo
  • 5,469
  • 1
  • 24
  • 19
13

Another option would be:

i.ToString("d3")
svick
  • 236,525
  • 50
  • 385
  • 514
  • @Brady Could you be more specific? It works fine for me. – svick Feb 12 '15 at 00:15
  • Sure. As soon as the compiler hits that line, the value doesn't change, it just goes right past it as if it wasn't there. `Dim thisDate As Date` `Dim thisMonth As Integer` `thisDate = Date.Today` `thisMonth = Month(thisDate)` `thisMonth.ToString("00")` = "2" for me instead of "02". – Brady Feb 12 '15 at 00:26
  • 3
    @Brady ToString doesn't *change* anything, it *returns* the string. Maybe that's the problem? (You don't actually show how you output the value, so it's hard to tell.) – svick Feb 12 '15 at 00:31
  • Ugh, I was trying to STORE the variable. Thank you for pointing me in the right direction! – Brady Feb 12 '15 at 00:37
  • 1
    For negative numbers, this will become 4 chars. For example, -10 becomes "-010" – VoteCoffee Apr 09 '17 at 04:01
7

I recall seeing code like this to pad numbers with zeros...

int[] nums = new int[] { 1, 10, 116 };

foreach (int i in nums)
{
    Console.WriteLine("{0:000}", i);
}

Output:

001
010
116
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
valacar
  • 71
  • 1
  • 1
    That works only if you want to output them to the console. What if you want to do something else with them? – svick Jun 19 '11 at 13:56
2

For the sake of completeness, this way is also possible and I prefere it because it is clearer and more flexible.

int value = 10;

// 010 
resultString = $"{value:000}";

// The result is: 010 
resultString = $"The result is: {value:000}";
marsh-wiggle
  • 2,508
  • 3
  • 35
  • 52
1

If we want to use it in a function with variable fixed length output, then this approach

public string ToString(int i, int Digits)
{
 return i.ToString(string.Format("D{0}", Digits));
}

runs 20% faster than this

return i.ToString().PadLeft(Digits, '0'); 

but if we want also to use the function with a string input (e.g. HEX number) we can use this approach:

public string ToString(string value, int Digits)
 {
 int InsDigits= Digits - value.Length;
 return ((InsDigits> 0) ? new String('0', InsDigits) + value : value);
 }
anefeletos
  • 672
  • 7
  • 19
-5

Every time I have needed to append things to the beginning of a string to match criteria like this I have used a while loop. Like so:

while (myString.length < 5) myString = "0" + myString;

Although there may be a string.format way to do this as well this has worked fine for me before.

MoarCodePlz
  • 5,086
  • 2
  • 25
  • 31