How to Left pad Zeros to integer to get String in Java

The below is the code as to how to add 0 Padding to the left of an integer to format it to string:

import java.text.DecimalFormat; 

public String padZeros(int i) { 
     DecimalFormat df = new DecimalFormat("00"); 
     return df.format(start); 

Change the Number format instead of “00” to any other format for getting more paddings.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.