Here you can find the source of getFormatWithMinimumDecimals(final int minimumDecimals, final int maximumDecimals)
Parameter | Description |
---|---|
minimumDecimals | the number of minimal visible decimals. |
maximumDecimals | the number of maximal visible decimals. |
public static DecimalFormat getFormatWithMinimumDecimals(final int minimumDecimals, final int maximumDecimals)
//package com.java2s; /*/*from ww w .j a v a 2 s .c om*/ * This file is part of Bukkit Plugin Utilities. * * Bukkit Plugin Utilities is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of the License, * or (at your option) any later version. * * Bukkit Plugin Utilities is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Bukkit Plugin Utilities. * If not, see <http://www.gnu.org/licenses/>. */ import java.text.DecimalFormat; import java.util.Arrays; public class Main { /** * Get a decimal format with a specific number of minimum and maximum * decimals. * * @param minimumDecimals * the number of minimal visible decimals. * @param maximumDecimals * the number of maximal visible decimals. * @return a decimal format with shows the specified minimum and maximum * decimals. * @since 1.3 */ public static DecimalFormat getFormatWithMinimumDecimals(final int minimumDecimals, final int maximumDecimals) { if (maximumDecimals < 0) { throw new IllegalArgumentException("Negative maximum decimals are invalid."); } else if (minimumDecimals < 0) { throw new IllegalArgumentException("Negative minimum decimals are invalid."); } else if (minimumDecimals > maximumDecimals) { throw new IllegalArgumentException("The minimum decimals have to be lower than the maximum decimals."); } else if (minimumDecimals == 0 && maximumDecimals == 0) { return new DecimalFormat("#0"); } else { char[] zeros = new char[maximumDecimals + 3]; Arrays.fill(zeros, '0'); Arrays.fill(zeros, minimumDecimals + 3, maximumDecimals + 3, '#'); // Always starts with: #0. zeros[0] = '#'; zeros[1] = '0'; zeros[2] = '.'; return new DecimalFormat(new String(zeros)); } } }