Java BigDecimal Format formatBigDecimal(BigDecimal bigDecimal, int scale)

Here you can find the source of formatBigDecimal(BigDecimal bigDecimal, int scale)

Description

Formate un big decimal

License

Open Source License

Parameter

Parameter Description
bigDecimal a parameter
scale a parameter

Declaration

public static String formatBigDecimal(BigDecimal bigDecimal, int scale) 

Method Source Code


//package com.java2s;
/*//from w w w .j  a  va2s .  co  m
 *
 * This file is part of Genome Artist.
 *
 * Genome Artist is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Genome Artist 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Genome Artist.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import java.util.Locale;

public class Main {
    public static final Locale LOCALE_RO = new Locale("ro", "RO");
    private static final String BASE_FORMATER_PATTERN = "##,##0";
    /**
     * Simbolurile asociate formaterului
     */
    private static DecimalFormatSymbols decimalFormatSymbols = null;
    /**
     * Formatez un numar la defaultul local
     * @param bigDecimal
     * @param scale
     * @return
     */
    private static DecimalFormat decimalFormater = null;
    private static int formaterScale = 0;

    /**
     * Formate un big decimal
     * @param bigDecimal
     * @param scale
     * @return
     */
    public static String formatBigDecimal(BigDecimal bigDecimal, int scale) {
        if (scale != formaterScale)
            initializeDecimalFormat(scale);

        return decimalFormater.format(bigDecimal.doubleValue());
    }

    /**
     * Initializez number formaterul
     * @param scale
     */
    private static void initializeDecimalFormat(int scale) {
        //Verific initializarea simbolurilor
        if (decimalFormatSymbols == null)
            initializeFormatSymbols();

        //Initializez formaterul
        if (formaterScale != scale) {
            String decimals = "";
            if (scale > 0)
                decimals = ".";
            for (int i = 0; i < scale; i++) {
                decimals += "0";
            }

            //Initializez formaterul
            decimalFormater = new DecimalFormat(BASE_FORMATER_PATTERN + decimals, decimalFormatSymbols);
        }
    }

    /**
     * Initializez simbolurile
     */
    private static void initializeFormatSymbols() {
        decimalFormatSymbols = new DecimalFormatSymbols(LOCALE_RO);
        decimalFormatSymbols.setDecimalSeparator('.');
        decimalFormatSymbols.setGroupingSeparator(',');
        decimalFormatSymbols.setMinusSign('-');
    }
}

Related

  1. formatAmount(final BigDecimal number)
  2. formatAsReadable(BigDecimal number)
  3. formatBetrag(BigDecimal bdBetrag, Locale locale)
  4. formatBigDecimal(BigDecimal bd, NumberFormat format)
  5. formatBigDecimal(BigDecimal bigDecimal)
  6. formatBigDecimal(BigDecimal d)
  7. formatBigDecimal(BigDecimal n, int dp)
  8. formatBigDecimal(BigDecimal number, int val)
  9. formatBigDecimal(BigDecimal valor, int decimal)