Java Number Format Pattern getFormatWithMinimumDecimals(final int minimumDecimals, final int maximumDecimals)

Here you can find the source of getFormatWithMinimumDecimals(final int minimumDecimals, final int maximumDecimals)

Description

Get a decimal format with a specific number of minimum and maximum decimals.

License

Open Source License

Parameter

Parameter Description
minimumDecimals the number of minimal visible decimals.
maximumDecimals the number of maximal visible decimals.

Return

a decimal format with shows the specified minimum and maximum decimals.

Declaration

public static DecimalFormat getFormatWithMinimumDecimals(final int minimumDecimals, final int maximumDecimals) 

Method Source Code

//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));
        }
    }
}

Related

  1. getFormatter()
  2. getFormatter()
  3. getFormatter(int precision)
  4. getFormatter(String pattern)
  5. getFormatTwoPoint(float param)
  6. getGeographicPositionFormatter()
  7. getIntegerFormat()
  8. getIntegerFormat(int maximumIntegerDigits)
  9. getIntegerFormatter()