Here you can find the source of formatIndex(int index, int totalSize)
Parameter | Description |
---|---|
index | the index to format. |
totalSize | the maximum value we're counting towards. |
index
.
public static String formatIndex(int index, int totalSize)
//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; } }