补充0

  1. 方法一:String.format()方法

  2. public class TestStringFormat {         

  3.   public static void main(String[] args) {         

  4.     int youNumber = 1;         

  5.     // 0 代表前面补充0         

  6.     // 4 代表长度为4         

  7.     // d 代表参数为正数型         

  8.     String str = String.format("%04d", youNumber);         

  9.     System.out.println(str);      

  10.   }         

  11. }       

  12. 方法二:用java中的DecimalFormat()方法

  13. private static final String STR_FORMAT = "0000";   

  14.   

  15. public static String test(String liuShuiHao){  

  16.     Integer intHao = Integer.parseInt(liuShuiHao);  

  17.     intHao++;  

  18.     DecimalFormat df = new DecimalFormat(STR_FORMAT);  

  19.     return df.format(intHao);  

  20. }  


你可能感兴趣的:(补充0)