Format number 11 as 00000011

Possible duplicate:
How to display the number "12" in the format "0000012"

I am trying to get a number to format myself within a predefined number of 000 (sorry if I'm not clear it is not easy to describe)

e.g. 112 would become 00000112

and e.g. 1 would become 00000001
+5
source share
8 answers

Try it like this: myNumber.ToString("D8");

+11
source
int i = 12;
var text = i.ToString("00000");
//text will be "00012"
+2
source

, 8

string.Format("{0:D8}", value);

value.ToString("D8");

.

+2
int number = 11;
string padded = number.ToString().PadLeft(8, '0');
+1
int foo = 11;
foo.ToString("D8");

- > 00000011

+1

String.PadLeft(), 0s ( ), String.Right(), , .

0

int number = 11;
string text = number.ToString("00000000");
0

You will need the String.PadLeft method, which aligns the string to the right. If the length is less than one of the numbers you give PadLeft, this will not change the value.

public string ZeroFill(int number, int length)
    return number.ToString().PadLeft(length, '0');
}
0
source

All Articles