How can I pad an integer with zeros on the left?

后端 未结 16 2299
南旧
南旧 2020-11-21 06:31

How do you left pad an int with zeros when converting to a String in java?

I\'m basically looking to pad out integers up to 9999

16条回答
  •  无人及你
    2020-11-21 06:48

    Use the class DecimalFormat, like so:

    NumberFormat formatter = new DecimalFormat("0000"); //i use 4 Zero but you can also another number
    System.out.println("OUTPUT : "+formatter.format(811)); 
    

    OUTPUT : 0000811

提交回复
热议问题