Java tutorial
//package com.java2s; //License from project: Apache License public class Main { public static String formatNum(int i1) { String s = String.valueOf(i1); if (i1 == 0) { s = "0000"; } else if (s.length() < 4) { switch (s.length()) { default: return s; case 1: // '\001' return (new StringBuilder()).append("000").append(s).toString(); case 2: // '\002' return (new StringBuilder()).append("00").append(s).toString(); case 3: // '\003' return (new StringBuilder()).append("0").append(s).toString(); } } return s; } }