Java BigDecimal Format formatoDecimal(String tipo, BigDecimal valor)

Here you can find the source of formatoDecimal(String tipo, BigDecimal valor)

Description

formato Decimal

License

Open Source License

Declaration

public static String formatoDecimal(String tipo, BigDecimal valor) 

Method Source Code

//package com.java2s;
/**//ww w . ja  va2 s . co  m
 * <p>Title: SIGEN</p> <p>Description: PAF-ECF + TEF - Biblioteca de
 * fun??es.</p>
 *
 * <p>The MIT License</p>
 *
 * <p>Copyright: Copyright (C) 2010 T2Ti.COM</p>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * The author may be contacted at: t2ti.com@gmail.com</p>
 *
 * @author Albert Eije (T2Ti.COM); Douglas Morato(ATD)
 * @version 1.0
 */

import java.math.BigDecimal;

import java.text.DecimalFormat;

public class Main {
    public static final int DECIMAL_QUANTIDADE = 2;
    public static final int DECIMAL_VALOR = 2;

    public static String formatoDecimal(String tipo, double valor) {

        String mascara = "0.";
        if (tipo.equals("Q")) {
            for (int i = 0; i < DECIMAL_QUANTIDADE; i++) {
                mascara += "0";
            }
        } else if (tipo.equals("V")) {
            for (int i = 0; i < DECIMAL_VALOR; i++) {
                mascara += "0";
            }
        }
        DecimalFormat formato = new DecimalFormat(mascara);
        return formato.format(valor);
    }

    public static String formatoDecimal(String tipo, BigDecimal valor) {

        String mascara = "0.";
        if (tipo.equals("Q")) {
            for (int i = 0; i < DECIMAL_QUANTIDADE; i++) {
                mascara += "0";
            }
        } else if (tipo.equals("V")) {
            for (int i = 0; i < DECIMAL_VALOR; i++) {
                mascara += "0";
            }
        }
        DecimalFormat formato = new DecimalFormat(mascara);
        return formato.format(valor);
    }
}

Related

  1. formatNumberByMaxFracUseGrp(BigDecimal number, int val)
  2. formatNumberCroreFormat(final BigDecimal num)
  3. formatNumberDouble(Double amount, int scale)
  4. formatNumberStr(String numberStr, Integer scale)
  5. formatNumer(BigDecimal value, int decimalPlaces, Locale locale, boolean roundCommercial)
  6. formatPercent(BigDecimal percent)
  7. formatPercentage(BigDecimal percentage)
  8. formatPercentDecimalDouble(Object input)
  9. formatQuantity(BigDecimal quantity)