My Technical Notes

Tuesday, 11 November 2014

Padding an Integer with Leading Zeros

In C#, to pad an integer with up to four leading zeros, we use the `D4` specifier:


int m = 23;
string mStr = m.ToString("D4"); // mStr == 0023

Another way, using `string.Format`:


string mStrFormat = string.Format("{0:D4}", m);

No comments: