补0
方式1
public class NumberFormatTest {
public static void main (String[] args) {
int i = 1 ;
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false );
nf.setMaximumIntegerDigits(4 );
nf.setMinimumIntegerDigits(4 );
System.out .println(nf.format(i));
}
}
方式2
/**
* Java里数字转字符串前面自动补0的实现。
*
*/
public class TestStringFormat {
public static void main (String[] args) {
int youNumber = 1 ;
String str = String.format("%04d" , youNumber);
System.out.println(str);
}
}
方式3
private static final String STR_FORMAT = "0000" ;
public static String haoAddOne_2 (String liuShuiHao){
Integer intHao = Integer.parseInt(liuShuiHao);
intHao++;
DecimalFormat df = new DecimalFormat(STR_FORMAT);
return df.format(intHao);
}
去开始0
方式一:
例如:”0000123 ” (字符串必须全为数字)
处理过程:
String tempStr = "0000123" ;
int result = Integer .parseInt(tempStr);
result 结果:123
方式二:
例如:”0000123 ”
处理过程:
String str = "0000123"
String newStr = str.replaceFirst ("^0*" , "" )
System.out .println (newStr)
打印结果:123
方式三:
例如:”0000123 ”
处理过程:
String str = "0000123"
String newStr = str.replaceAll ("^(0+)" , "" )
System.out .println (newStr)
打印结果:123