C# int ToString format on 2 char int?

Ray picture Ray · Feb 14, 2012 · Viewed 51.7k times · Source

How do I use the ToString method on an integer to display a 2-char

int i = 1; i.ToString() -> "01" instead of "1"

Thanks.

Answer

Patrick McDonald picture Patrick McDonald · Feb 14, 2012

You can use i.ToString("D2") or i.ToString("00")

See Standard Numeric Format Strings and Custom Numeric Format Strings on Microsoft Docs for more details