Java Number Format formatNumber(long pNum, int pLen)

Here you can find the source of formatNumber(long pNum, int pLen)

Description

Formats a number with leading zeroes, to a specified length.

License

Open Source License

Parameter

Parameter Description
pNum The number to format
pLen The number of digits

Exception

Parameter Description
IllegalArgumentException Thrown, if the number containsmore digits than allowed by the length argument.

Return

A string containing the formatted number

Declaration



static String formatNumber(long pNum, int pLen) throws IllegalArgumentException 

Method Source Code

//package com.java2s;

public class Main {
    /**//w  ww . j  a  va2  s. c  o m
     * Formats a number with leading zeroes, to a specified length.
     *
     * @param pNum The number to format
     * @param pLen The number of digits
     * @return A string containing the formatted number
     * @throws IllegalArgumentException Thrown, if the number contains
     *                                  more digits than allowed by the length argument.
     * @see #pad(String,int,String,boolean)
     * @deprecated Use StringUtil.pad instead!
     */

    /*public*/
    static String formatNumber(long pNum, int pLen) throws IllegalArgumentException {
        StringBuilder result = new StringBuilder();

        if (pNum >= Math.pow(10, pLen)) {
            throw new IllegalArgumentException(
                    "The number to format cannot contain more digits than the length argument specifies!");
        }
        for (int i = pLen; i > 1; i--) {
            if (pNum < Math.pow(10, i - 1)) {
                result.append('0');
            } else {
                break;
            }
        }
        result.append(pNum);
        return result.toString();
    }
}

Related

  1. formatNumber(Integer i, int bisectNumber)
  2. FormatNumber(Integer pNumber, int tolLen)
  3. formatNumber(long i, int digits)
  4. formatNumber(long number)
  5. formatNumber(long number, int digits)
  6. formatNumber(long value)
  7. formatNumber(Number number)
  8. formatNumber(Object num)
  9. formatNumber(String formatStr, int length, boolean leftPadding, char paddingCharacter)