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,030
  • 21
  • 100
  • 124
pearcewg
  • 9,515
  • 20
  • 77
  • 123

7 Answers7

58

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
  • 133,702
  • 30
  • 291
  • 279
26

Take a look at PadLeft.

ex:

int i = 40;

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

s == "040"

svick
  • 225,720
  • 49
  • 378
  • 501
ingo
  • 5,289
  • 1
  • 23
  • 18
12

Another option would be:

i.ToString("d3")
svick
  • 225,720
  • 49
  • 378
  • 501
  • @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
  • 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,030
  • 21
  • 100
  • 124
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,123
  • 3
  • 29
  • 46
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
  • 605
  • 7
  • 17
-3

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
  • 4,915
  • 2
  • 22
  • 32