本文介绍了从字符串值开始数字递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我出于某种原因申请了我的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位数字.

"D"(或十进制)格式说明符

这篇关于从字符串值开始数字递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:47
查看更多