Java BigDecimal Format formatBigDecimal(BigDecimal value, char thousandSep, char decimalPoint, int numDecimals)

Here you can find the source of formatBigDecimal(BigDecimal value, char thousandSep, char decimalPoint, int numDecimals)

Description

Format a BigDecimal, grouping thousands and rounding to a specified number of decimal digits.

License

Open Source License

Parameter

Parameter Description
value The BigDecimal to format.
thousandSep The separator for groups of thousands to be used.
decimalPoint The character for the decimal point to be used.
numDecimals The number of decimal places. Uses ROUND_HALF_UP if necessary. If numDecimals is smaller than 0, 'value' is output with the number of decimal places according to its scale.

Return

The formatted BigDecimal.

Declaration

public static String formatBigDecimal(BigDecimal value, char thousandSep, char decimalPoint, int numDecimals) 

Method Source Code

//package com.java2s;
/*//  w ww.  j a  v a  2  s. c  om
 * This is a class containing various static utility methods.
 *
 * Copyright (C) 1999 Thomas Studer
 * mailto:tstuder@datacomm.ch
 * http://www.datacomm.ch/tstuder
 *
 * This class 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 2 of the License, or (at your option) any later version.
 *
 * This class 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 this class; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.math.BigDecimal;

public class Main {
    /**
     * Format a BigDecimal, grouping thousands and rounding to a specified number
     * of decimal digits.
     * 
     * @return The formatted BigDecimal.
     *
     * @param value The BigDecimal to format.
     * @param thousandSep The separator for groups of thousands to be used.
     * @param decimalPoint The character for the decimal point to be used.
     * @param numDecimals The number of decimal places. Uses ROUND_HALF_UP if
     * necessary. If numDecimals is smaller than 0,
     * 'value' is output with the number of decimal places according to its scale.
     */
    public static String formatBigDecimal(BigDecimal value, char thousandSep, char decimalPoint, int numDecimals) {

        if (numDecimals >= 0 && value.scale() != numDecimals) {
            value = value.setScale(numDecimals, BigDecimal.ROUND_HALF_UP);
        }

        String stringValue = value.toString();
        int origLen = stringValue.length();
        int decPoint = stringValue.indexOf('.');
        int thousandSepCount;
        boolean hasDecPoint;
        boolean isNegative;
        int mag;

        if (decPoint < 0) {
            decPoint = origLen;
            hasDecPoint = false;
        } else {
            // Eliminate trailing zeros
            hasDecPoint = true;
            if (numDecimals == -1) {
                while (stringValue.charAt(origLen - 1) == '0') {
                    origLen--;
                }
                if (stringValue.charAt(origLen - 1) == '.') {
                    origLen--;
                    hasDecPoint = false;
                }
            }
        }

        if (stringValue.charAt(0) == '-') {
            isNegative = true;
            thousandSepCount = (decPoint - 2) / 3;
            mag = decPoint - 2;
        } else {
            isNegative = false;
            thousandSepCount = (decPoint - 1) / 3;
            mag = decPoint - 1;
        }

        StringBuffer buf = new StringBuffer(origLen + thousandSepCount);
        int srcIndex = 0;

        if (isNegative) {
            srcIndex++;
            buf.append('-');
        }

        // Kopiere Vorkommastellen und fuege Tausendertrenner ein wo noetig.
        while (srcIndex < decPoint) {
            buf.append(stringValue.charAt(srcIndex));
            srcIndex++;
            if (mag > 0 && mag % 3 == 0)
                buf.append(thousandSep);
            mag--;
        }

        // Kopiere Nachkommastellen
        if (hasDecPoint) {
            srcIndex++;
            buf.append(decimalPoint);
            while (srcIndex < origLen) {
                buf.append(stringValue.charAt(srcIndex));
                srcIndex++;
            }
        }

        return buf.toString();
    }
}

Related

  1. formatBigDecimal(BigDecimal bigDecimal, int scale)
  2. formatBigDecimal(BigDecimal d)
  3. formatBigDecimal(BigDecimal n, int dp)
  4. formatBigDecimal(BigDecimal number, int val)
  5. formatBigDecimal(BigDecimal valor, int decimal)
  6. formatBigDecimal(String value)
  7. formatBigDecimalWithPrecise(BigDecimal number)
  8. formatCents(int cents)
  9. formatCUBRIDNumber(BigDecimal value)