Simple written
String.format ( "% 04d", 3 )
However, I am surprised that StringBuilder + replace actually higher efficiency
public class Test { public static String format2(int shortval) { String formatted = Integer.toString(shortval); StringBuilder buf = new StringBuilder("0000"); buf.replace(4 - formatted.length(), 4, formatted); return buf.toString(); } public static void main(String[] args) { String s; { long start = System.nanoTime(); int cnt = 100_0000; while (cnt-- > 0) { s = String.format("%04d", 3); } long end = System.nanoTime(); System.out.println(end - start); } { long start = System.nanoTime(); int cnt = 100_0000; while (cnt-- > 0) { s = format2((short) 3); } String a =""; long end = System.nanoTime(); System.out.println(end - start); } } }