Java Fraction Format formatoDecimal(String tipo, double valor)

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

Description

formato Decimal

License

Open Source License

Declaration

public static String formatoDecimal(String tipo, double valor) 

Method Source Code

//package com.java2s;
/**/*from w w w.j a v a  2  s . com*/
 * <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. formatNumber(String format, double number)
  2. formatNumber(String number, int maxFractionalDigits)
  3. formatNumberAttribute(double v)
  4. formatNumFraction(float num, int unit)
  5. formatNumValueWithComma(double val)
  6. formatOLETime(double oleTime)
  7. formatOneDecimal(double dNumber)
  8. formatOneDp(double inNumber)
  9. formatPair(double in, double out)