我为Dotnet狂

------学而不思则罔,思而不学则殆

 

[C#]将数字前面补0,补足设定的长度

[C#]將數字前面補0,補足設定的長度

這是在小舖遇到的問題,如何將數字前面補0,補足設定的長度

例如 將 123 前面補0,補到長度為6,可以透過 String.PadLeft 與 String.Format 的方式

http://msdn.microsoft.com/zh-tw/library/system.string.padleft(VS.80).aspx

http://msdn.microsoft.com/zh-tw/library/system.string.format(VS.80).aspx

// 將數字前面補0,補足長度為6
String snum = "123";
String pnum = snum.PadLeft(6, '0');
String fnum = String.Format("{0:000000}",Convert.ToInt16(snum));
MessageBox.Show( "原始字串 : " + snum + "\n 透過 PadLeft : " + pnum + "\n 透過 String.Format : " + fnum);

執行結果

參考

http://www.blueshop.com.tw/board/show.asp?subcde=BRD20090319214030QIF&fumcde=

http://www.cnblogs.com/tuyile006/archive/2006/07/13/449884.aspx

posted on 2009-11-06 08:47  3stones  阅读(574)  评论(0编辑  收藏  举报

导航