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

后端 未结 16 2284
南旧
南旧 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:47

    Try this one:

    import java.text.DecimalFormat; 
    
    DecimalFormat df = new DecimalFormat("0000");
    
    String c = df.format(9);   // Output: 0009
    
    String a = df.format(99);  // Output: 0099
    
    String b = df.format(999); // Output: 0999
    

提交回复
热议问题