Java String Pad Left lpad(int maxDigits, long val)

Here you can find the source of lpad(int maxDigits, long val)

Description

lpad

License

LGPL

Declaration

public static String lpad(int maxDigits, long val) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static String lpad(int maxDigits, long val) {
        int ndigits = ndigits(val);
        int pad = maxDigits - ndigits;
        StringBuilder buf = new StringBuilder();
        while (pad > 0) {
            buf.append('0');
            pad--;// ww  w  . j a v a2s  .c om
        }
        buf.append(val);
        return buf.toString();
    }

    private static int ndigits(long val) {
        val = Long.highestOneBit(val);
        int i = 0;
        while (val > 0) {
            i++;
            val /= 10;
        }
        return i;
    }
}

Related

  1. leftZeroPadding(int number, int howManyChar)
  2. lpad(final String input, final String padCode, final int toLength)
  3. lpad(final String s, final String pad, final int size)
  4. lpad(int i, int length, char c)
  5. lpad(int length, long number)
  6. lpad(int value, int padLen, char padChar)
  7. lpad(Object str, int len, String padding)
  8. lpad(String base, int len, String pad)
  9. lpad(String input, char padding, int length)