Java Number Format Pattern stringToReais(String unformatted, boolean comSimbolo)

Here you can find the source of stringToReais(String unformatted, boolean comSimbolo)

Description

string To Reais

License

Apache License

Declaration

public static String stringToReais(String unformatted, boolean comSimbolo) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;

import java.util.Locale;

public class Main {
    public static String stringToReais(String unformatted, boolean comSimbolo) {

        DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale("pt", "BR"));
        symbols.setGroupingSeparator('.');
        symbols.setDecimalSeparator(',');

        if (!comSimbolo) {
            symbols.setCurrencySymbol("");
        }//from   ww  w  .j av  a 2 s  .  co m

        DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
        //      DecimalFormat decimalFormat= new DecimalFormat("#,##0.0#", symbols); <-- erra
        decimalFormat.setDecimalFormatSymbols(symbols);
        decimalFormat.setParseBigDecimal(true);
        decimalFormat.setMinimumFractionDigits(2);

        return decimalFormat.format(unformatted).trim();

    }
}

Related

  1. print(float[] array, NumberFormat nf)
  2. resetDecimalFormat()
  3. resetDecimalFormatByLocale(Locale locale)
  4. roundNumber(Number number, DecimalFormat format)
  5. stringFormat(BigDecimal value)
  6. strToFormatedNumber(String str)
  7. toAccountantFormat(String str, int scale)
  8. toDecimalFormat(BigDecimal value)
  9. toFormattedNumber(Object value)