Java Integer to intToStringWithZeroFill(int intValue, int width)

Here you can find the source of intToStringWithZeroFill(int intValue, int width)

Description

Convert integer to string with left zero fill.

License

Open Source License

Parameter

Parameter Description
intValue The integer to convert.
width Width of result field.

Return

The integer converted to a string with enough leading zeros to fill the specified field width.

Declaration


public static String intToStringWithZeroFill(int intValue, int width) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

public class Main {
    /** Convert integer to string with left zero fill.
     */*from   w  w w  .  ja v a  2s  . c om*/
     *   @param   intValue   The integer to convert.
     *   @param   width      Width of result field.
     *
     *   @return            The integer converted to a string
     *                  with enough leading zeros to fill
     *                  the specified field width.
     */

    public static String intToStringWithZeroFill(int intValue, int width) {
        String s = new Integer(intValue).toString();

        if (s.length() < width)
            s = dupl('0', width - s.length()) + s;

        return s;
    }

    /** Duplicate character into string.
     *
     *   @param   ch      The character to be duplicated.
     *   @param   n      The number of duplicates desired.
     *
     *   @return         String containing "n" copies of "ch".
     *
     *   <p>
     *   if n <= 0, the empty string "" is returned.
     *   </p>
     */

    public static String dupl(char ch, int n) {
        if (n > 0) {
            StringBuffer result = new StringBuffer(n);

            for (int i = 0; i < n; i++) {
                result.append(ch);
            }

            return result.toString();
        } else {
            return "";
        }
    }

    /** Duplicate string into string.
     *
     *   @param   s      The string to be duplicated.
     *   @param   n      The number of duplicates desired.
     *
     *   @return         String containing "n" copies of "s".
     *
     *   <p>
     *   if n <= 0, the empty string "" is returned.
     *   </p>
     */

    public static String dupl(String s, int n) {
        if (n > 0) {
            StringBuffer result = new StringBuffer(n);

            for (int i = 0; i < n; i++) {
                result.append(s);
            }

            return result.toString();
        } else {
            return "";
        }
    }
}

Related

  1. intToScaleString(final int number, final int scale)
  2. intToShort(final int value)
  3. intToSlider(final int min, final int max, final int value)
  4. intToSortableBytes(int value, byte[] result, int offset)
  5. intToStringBuffer(final int param, final int len)
  6. intToTime(int time)
  7. intToTime(int value)
  8. intToTriplePlace(int i)
  9. intToTwoByte(int value, byte[] destination, int offset)