生成6位数字

时间:2013-03-27 09:06:58

标签: c#

我正从数据库中检索一些记录,我追加到某些字符串 - 例如:

spouse counter=1;
counter=1+counter;

然后我通过以下方式得到customerID

customerID = "AGP-00000" + counter;

以便customerIDAGP-000001

对于计数器值2 - customerID字符串将为AGP-000002

我的问题是,如果计数器为10,则customer ID将为AGP-0000010,我希望它为AGP-000010

如何确保customer ID始终像AGP-000010

2 个答案:

答案 0 :(得分:12)

int  number = 1;
string customerID = "AGP-" + number.ToString("D6");

customerID将是:"AGP-000001"

请参阅:How to: Pad a Number with Leading Zeros

答案 1 :(得分:0)

不确定这是否是最有效的方式,但它有效

string str = "AGP-00000";
int counter = 100;
str = str.Substring(0, str.Length - counter.ToString().Length + 1);
str += counter.ToString();