Here you can find the source of zeroPad(int n, int base, int width)
Parameter | Description |
---|---|
n | The integer. |
base | The base, either 10 or 16. If 16, any values 10-15 are returned as upper-case A-F. |
width | The field width, maximum 8. |
public static String zeroPad(int n, int base, int width)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w w w . ja va2 s. c o m*/ * Zero-pad an integer. * * @param n The integer. * @param base The base, either 10 or 16. If 16, any values 10-15 are * returned as upper-case A-F. * @param width The field width, maximum 8. * @return Returns a string representation of the integer zero-padded to * the given width. */ public static String zeroPad(int n, int base, int width) { final String s; switch (base) { case 10: s = Integer.toString(n); break; case 16: s = Integer.toHexString(n).toUpperCase(); break; default: throw new IllegalArgumentException("base must be 10 or 16"); } final int len = s.length(); return len < width ? "0000000".substring(7 - width + len) + s : s; } }