从字符串值开始数字递增 [英] Number increment from string value
问题描述
我出于某种原因申请了我的5位两个数字.
I my application due to some reason I have two numbers in 5 digits.
以下代码为您简要介绍.
The following code give you brief idea.
string s = "00001"; // Initially stored somewhere.
//Operation start
string id = DateTime.Now.ToString("yy") + DateTime.Now.AddYears(-1).ToString("yy") + s;
//Operation end
//Increment the value of s by 1. i.e 00001 to 00002
通过将 s 的值转换为int并将其递增1即可轻松完成,但毕竟我还必须将s的递增值存储在5位数字中,因此它将是"00002" .
This can be done easily by convert the value of s to int and increment it by 1 but after all that I have to also store the incremented value of s in 5 digit so it will be "00002".
这种想法让我很痛苦...
This think give me a pain...
推荐答案
使用
string s = "00001";
int number = Convert.ToInt32(s);
number += 1;
string str = number.ToString("D5");
至少获得5位数字.
如果需要,该数字将在其左侧填充零以产生精度说明符给出的数字位数.如果没有指定了精度说明符,默认值为最小值需要代表不带前导零的整数.
If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. If no precision specifier is specified, the default is the minimum value required to represent the integer without leading zeros.
这篇关于从字符串值开始数字递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!