Java中数字前补零的方法,数字加减前补零函数

 
  1.  * 将元数据前补零,补后的总长度为指定的长度,以字符串的形式返回

  2.  
  3.   * @param sourceDate

  4.  
  5.   * @param formatLength

  6.  
  7.   * @return 重组后的数据

  8.  
  9.   */

  10.  
  11.   public static String frontCompWithZore(int sourceDate,int formatLength)

  12.  
  13.   {

  14.  
  15.   /*

  16.  
  17.   * 0 指前面补充零

  18.  
  19.   * formatLength 字符总长度为 formatLength

  20.  
  21.   * d 代表为正数。

  22.  
  23.   */

  24.  
  25.   String newString = String.format("%0"+formatLength+"d", sourceDate);

  26.  
  27.   return newString;

  28.  
  29.   }

如:

 
  1. String newString = String.format("%02d", 5);

  2. System.out.println("newString === "+newString);


运行结果:

newString === 05

猜你喜欢

转载自blog.csdn.net/axiaositong/article/details/81974682