1. numIncre()
返回同闊度數字型字串. 例:
numIncre(“000000”) => “000001”
numIncre(”0023") => “0024”
numIncre(“0009”) => “0010”
numIncre(“000099”) => “000100”public static String numIncre(String num);
返回同闊度數字型字串. 例:
numIncre(“000000”) => “000001”
numIncre(”0023") => “0024”
numIncre(“0009”) => “0010”
numIncre(“000099”) => “000100”public static String numIncre(String num);
public class numberUtil {
public static String numIncre(String num) {
int incre = Integer.parseInt(num) + 1;
return String.format("%0"+num.length()+"d", incre);
} /**
* <p>功能描述:[方法功能中文描述]</p>
* @param args
* @author:jack
* @update:[日期YYYY-MM-DD] [更改人姓名][变更描述]
*/ public static void main(String[] args) {
System.out.println(numberUtil.numIncre("000099"));
}}
*
* @author Zang XT
*/
public class TestQueue {
public static void main(String[] args) {
System.out.println(numIncre("000099"));
System.out.println(numIncre("000000"));
System.out.println(numIncre("0023"));
System.out.println(numIncre("9999"));
System.out.println(numIncre("0009"));
}
public static String numIncre(String str){
if(str==null){
return str;
}
int length = str.length();
int num = Integer.parseInt(str);
num++;
str = String.valueOf(num);
int length2 = str.length();
//需要在前面补充0
if(length2<length)
{
StringBuilder builder =new StringBuilder();
for(int i=0;i<length-length2;i++){
builder.append('0');
}
str = builder.toString()+str;
}
return str;
}
}
这种方法不错我刚开始写的是这种
public class numIncrement { public static String numIncre(String num) {
Integer b = Integer.parseInt(num) + 1;
String c = b.toString();
int ilen = b.toString().length();
int len = num.length();
for (; ilen < len; ilen++) { c = "0" + c; } return c; } public static void main(String[] args) { String a = numIncre("000099");
System.out.println(a); }}
{
char [] buffer = num.toCharArray();
int i = 0;
// 进位标志
boolean flag = false;
// 从右向左扫描
for (int i = buffer.length - 1; i >= 0; i--)
{
flag = false;
if (++buffer[i] > '9') // 要进位
{
flag = true;
buffer[i] = '0';
}
if (!flag) // 进位完毕
{
break;
}
}
String rs = new String(buffer);
if (flag && i == -1) // 最大一位结束后还在进位
{
rs = "1" + rs;
}
return rs;
}