有時候我們希望統一輸出的字串格式,例如在個位數前面補0,或希望統一長度 3位數,因此在不足3位數時補0,例如結果 3 ,長度為3位數,不足補0,輸出的結果就是 003 ,此時我們有二個方式可以達到這個目的
1)string.PadLeft 2)string.Format
Sample Code
string intValue = "2"; Console.Write("PadLeft Result :" + intValue.PadLeft(5,'0')); Console.WriteLine(); Console.Write("string.Format Result :" + string.Format("{0:00000}",Convert.ToInt16(intValue))); Console.ReadLine();