Here you can find the source of zeroFill(int num, int size, String character)
Parameter | Description |
---|---|
num | a parameter |
size | a parameter |
public static final String zeroFill(int num, int size, String character)
//package com.java2s; // it under the terms of the GNU General Public License as published by public class Main { /**/*w ww .ja v a 2 s . c om*/ * Takes the number num, and displays it with size digits. * * If num has more than size digits, it is truncated on the left : * addZeros(1907,2) -> "07" * * If it has less, the left-side is filled with '0's : addZeros(777, 8) -> * "00000777" * * @param num * @param size * @return a String as described above. */ public static final String zeroFill(int num, int size) { return zeroFill(num, size, "0"); } /** * Takes the number num, and displays it with size digits. * * If num has more than size digits, it is truncated on the left : * addZeros(1907,2) -> "07" * * If it has less, the left-side is filled with '0's : addZeros(777, 8) -> * "00000777" * * @param num * @param size * @return a String as described above. */ public static final String zeroFill(int num, int size, String character) { StringBuilder res = new StringBuilder(); String snum = "" + num; for (int i = snum.length(); i < size; i++) { res.append(character); } res.append(snum); return res.toString().substring(res.length() - size); } }