Zero-padding Integers

Submitted by:Jhon Brain

Date added:28 January, 2013

Category:Java

Converts integer to left-zero padded string, len chars long.

Tags: zero padding

Code Snippet:

public static String toLZ( int i, int len ) {
String s = Integer.toString(i);
if ( s.length() > len ) return s.substring(0,len);
else if ( s.length() < len ) // pad on left with zeros
return "000000000000000000000000000".substring(0, len - s.length ()) s;
else return s;
}
 
 

Comments