Here you can find the source of formatNumber(long pNum, int pLen)
Parameter | Description |
---|---|
pNum | The number to format |
pLen | The number of digits |
Parameter | Description |
---|---|
IllegalArgumentException | Thrown, if the number containsmore digits than allowed by the length argument. |
static String formatNumber(long pNum, int pLen) throws IllegalArgumentException
//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(); } }