Java Zero Format zeroFill(int num, int size, String character)

Here you can find the source of zeroFill(int num, int size, String character)

Description

Takes the number num, and displays it with size digits.

License

Open Source License

Parameter

Parameter Description
num a parameter
size a parameter

Return

a String as described above.

Declaration

public static final String zeroFill(int num, int size, String character) 

Method Source Code

//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);
    }
}

Related

  1. zeroes(final int ch)
  2. zeroEsquerda(String s1, int tamString)
  3. zeroExtend(long value, int inputBits)
  4. zeroFill(float[] array)
  5. zeroFill(int iv, int len)
  6. zeroFill(int value, int fieldWidth)
  7. zerofill(int x, int desiredWidth)
  8. zeroFillString(String originalString, int numZeros)
  9. zeroFormat(Integer source)