這是在小舖遇到的問題,如何將數字前面補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
本文介绍在C#中如何将数字字符串前置补零至指定长度的方法,提供了使用String.PadLeft及String.Format两种实现方式,并附带示例代码。

5690

被折叠的 条评论
为什么被折叠?



