Convert integer number to three digit

user1270940 picture user1270940 · May 31, 2012 · Viewed 22.5k times · Source

I have an integer variable .if its 1-9 it only displays as "1" or "9", I'm looking to convert the variable to save as 3 digits, ie. "001", or "009", etc. any ideas? I am using C#,ASP.Net

Answer

Yahia picture Yahia · May 31, 2012

use

int X = 9;

string PaddedResult = X.ToString().PadLeft (3, '0'); // results in 009

see MSDN references here and here.