Java Long to longToStringWithZeroFill(long longValue, int width)

Here you can find the source of longToStringWithZeroFill(long longValue, int width)

Description

Convert long to string with left zero fill.

License

Open Source License

Parameter

Parameter Description
longValue The long to convert.
width Width of result field.

Return

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

Declaration


public static String longToStringWithZeroFill(long longValue, int width) 

Method Source Code

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

public class Main {
    /** Convert long to string with left zero fill.
     *//  w  w  w .j  a v a 2  s  .  co  m
     *   @param   longValue   The long to convert.
     *   @param   width      Width of result field.
     *
     *   @return            The long converted to a string
     *                  with enough leading zeros to fill
     *                  the specified field width.
     */

    public static String longToStringWithZeroFill(long longValue, int width) {
        String s = new Long(longValue).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. longToPrefixCoded(final long val, final int shift, final char[] buffer)
  2. longToRegisters(long v)
  3. longToRemoteAddrTo(long remoteAddr)
  4. longToSortableBytes(long value, byte[] result, int offset)
  5. longToStringAddress(long addr)
  6. longToTimeString(long time)
  7. longToX(double longitudeDegrees, double radius)
  8. longToX(double longitudeDegrees, double radius)
  9. longToX(final double longitude, final int zoom)