Java File Size Readable Format formatIndex(int index, int totalSize)

Here you can find the source of formatIndex(int index, int totalSize)

Description

Format a number as String that can be always be large enough to hold "totalSize" digits.

License

Open Source License

Parameter

Parameter Description
index the index to format.
totalSize the maximum value we're counting towards.

Return

a String representation of index.

Declaration

public static String formatIndex(int index, int totalSize) 

Method Source Code

//package com.java2s;
/**//  w ww .j  ava2  s.c o m
 * This software is released as part of the Pumpernickel project.
 * 
 * All com.pump resources in the Pumpernickel project are distributed under the
 * MIT License:
 * https://raw.githubusercontent.com/mickleness/pumpernickel/master/License.txt
 * 
 * More information about the Pumpernickel project is available here:
 * https://mickleness.github.io/pumpernickel/
 */

public class Main {
    /**
     * Format a number as String that can be always be large enough to hold
     * "totalSize" digits. So for example: "1" should be formatted as "001" if
     * there will eventually be "235" elements, but it should be formatted as
     * "01" if there will only be "23" elements.
     * 
     * @param index
     *            the index to format.
     * @param totalSize
     *            the maximum value we're counting towards.
     * @return a String representation of <code>index</code>.
     */
    public static String formatIndex(int index, int totalSize) {
        int digits = (int) (Math.ceil(Math.log(totalSize) / Math.log(10)) + .5);
        String s = "" + index;
        while (s.length() < digits) {
            s = "0" + s;
        }
        return s;
    }
}

Related

  1. formatFileSize(long size, String format)
  2. FormatFileSize(String filesize)
  3. formatFilesize(String filesize, int decimalPlaces)
  4. formatFileStats(final String label, final long fileCount, final Object rawSize)
  5. formatGroup(String str, int groupSize, int lineBreak)
  6. formatInt(int val, int size)
  7. formatRow(String[] values, int[] sizes)
  8. formatSeperatorRow(int[] sizes)
  9. formatShortByte(long size)