Java BigDecimal Format formatNumer(BigDecimal value, int decimalPlaces, Locale locale, boolean roundCommercial)

Here you can find the source of formatNumer(BigDecimal value, int decimalPlaces, Locale locale, boolean roundCommercial)

Description

format Numer

License

Open Source License

Parameter

Parameter Description
value a parameter
decimalPlaces a parameter
locale a parameter
roundCommercial a parameter

Declaration

public static String formatNumer(BigDecimal value, int decimalPlaces, Locale locale, boolean roundCommercial) 

Method Source Code


//package com.java2s;
/*//  w w w .  j a v a2 s  . co m
 * Utility class to simplify the use of BigDecimals
 * Copyright (C) 2012 Martin Absmeier, IT Consulting Services
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.math.BigDecimal;

import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Locale;

public class Main {
    /**
     * 
     * @param value
     * @param decimalPlaces
     * @param locale
     * @param roundCommercial
     * @return
     */
    public static String formatNumer(BigDecimal value, int decimalPlaces, Locale locale, boolean roundCommercial) {
        DecimalFormat df = getDecimalFormat(decimalPlaces, locale);
        if (roundCommercial) {
            return df.format(roundCommercial(value, decimalPlaces));
        }
        return df.format(value);
    }

    private static DecimalFormat getDecimalFormat(int decimalPlaces, Locale locale) {
        StringBuffer sb = new StringBuffer();

        sb.append("#,###,##0.");
        for (int i = 0; i < decimalPlaces; i++) {
            sb.append('0');
        }
        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(locale);
        df.applyPattern(sb.toString());

        return df;
    }

    /**
     * 
     * @param value
     * @param decimalPlaces
     * @return
     */
    public static BigDecimal roundCommercial(BigDecimal value, int decimalPlaces) {
        return value.round(getMathContext(decimalPlaces));
    }

    private static MathContext getMathContext(int decimalPlaces) {
        return new MathContext(decimalPlaces, RoundingMode.HALF_UP);
    }
}

Related

  1. formatNumberByMaxFrac(BigDecimal number, int val)
  2. formatNumberByMaxFracUseGrp(BigDecimal number, int val)
  3. formatNumberCroreFormat(final BigDecimal num)
  4. formatNumberDouble(Double amount, int scale)
  5. formatNumberStr(String numberStr, Integer scale)
  6. formatoDecimal(String tipo, BigDecimal valor)
  7. formatPercent(BigDecimal percent)
  8. formatPercentage(BigDecimal percentage)
  9. formatPercentDecimalDouble(Object input)