/**

*将数字的左边补0

*/

@Test

publicvoidsumberFormatTest(){

//待测试数据

inti=1;

//得到一个NumberFormat的实例

NumberFormatnf=NumberFormat.getInstance();

//设置是否使用分组

nf.setGroupingUsed(false);

//设置最大整数位数

nf.setMaximumIntegerDigits(2);

//设置最小整数位数

nf.setMinimumIntegerDigits(2);

//输出测试语句

System.out.println(nf.format(i));

}

   

   

/**

*Java里数字转字符串前面自动补0的实现

*/

@Test

publicvoidstringFormatTest(){

intyouNumber=1;

//0代表前面补充0

//2代表长度为2

//d代表参数为正数型

Stringstr=String.format("%02d",youNumber);

System.out.println(str);//0001

   

}

   

/**

*格式化流水单号

*/

@Test

publicvoidorderNum(){

//流水号加1后返回,流水号长度为4

finalStringSTR_FORMAT="0000";

StringliuShuiHao="12";

IntegerintHao=Integer.parseInt(liuShuiHao);

intHao++;

DecimalFormatdf=newDecimalFormat(STR_FORMAT);

System.out.println(df.format(intHao));

   

}

相关文章:

  • 2022-12-23
  • 2021-09-27
  • 2021-09-29
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2022-12-23
相关资源
相似解决方案