0

I'm using a class to format the value of my EditText to currency. This class use the function NumberFormat.getCurrencyInstance().format((parsed/100)); to format. This class make my value with two decmal places (R$2,00). I want it to have three decimal places (R$2,000). Its for gas value. Here in Brasil we use three decimal places for gas.

This is the class I'm using:

public class MascaraMonetaria implements TextWatcher{

final EditText mEditText;
String current;
static Context context;
public MascaraMonetaria(EditText mEditText, String current, Context context) {
    super();
    this.mEditText = mEditText;
    this.current = current;     
}

@Override
public void afterTextChanged(Editable arg0) {}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}

@Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
    if (!s.toString().equals(current)) {
        mEditText.removeTextChangedListener(this);

        String cleanString = s.toString().replaceAll("[R$,.]", "");

        double parsed = Double.parseDouble(cleanString);
        String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));

        current = formatted;

        mEditText.setText(formatted);
        mEditText.setSelection(formatted.length());
        mEditText.addTextChangedListener(this);
    }

}

public static double stringMonetarioToDouble(String str) {
    double retorno = 0;
    try {
        boolean hasMask = ((str.indexOf("R$") > -1 || str.indexOf("$") > -1) && (str
                .indexOf(".") > -1 || str.indexOf(",") > -1));
        // Verificamos se existe máscara
        if (hasMask) {
            // Retiramos a mascara.
            str = str.replaceAll("[R$]", "").replaceAll("[$]", "").replaceAll("[.]", "").replaceAll("[,]", ".");
        }    
        // Transformamos o número que está escrito no EditText em double.
        retorno = Double.parseDouble(str);

    } catch (NumberFormatException e) {

    }
    return retorno;

}

}

Roland
  • 826
  • 2
  • 9
  • 24

3 Answers3

1

I did it I use the code by this answer: Correctly formatting currencies to more decimal places than the Locale specifies

This is my code:

        formatted.setMinimumFractionDigits(3);
        formatted.setMaximumFractionDigits(3);

        current = formatted.format((parsed/1000));

        mEditText.setText(formatted.format((parsed/1000)));
        mEditText.setSelection(formatted.format((parsed/1000)).length());
        mEditText.addTextChangedListener(this);
Community
  • 1
  • 1
Roland
  • 826
  • 2
  • 9
  • 24
0

You could just create a decimal formatter to three decimal places and concat a '$' sign onto it where needed.

You could also do all of this from a function, and then call that function where needed.

   import java.text.DecimalFormat;

   DecimalFormat fmt = new DecimalFormat("0.###");

   public String currencyConverter(int money);
   {
       String cash = Integer.toString(money);

      return "$" + fmt.format(cash);


   }

or you could try this.

   DecimalFormat fmt = new DecimalFormat("$0.###");

I'm not sure if it will work and I have no way to test it ATM however.

David MacNeil
  • 126
  • 11
0

You can set scale to format the price value as shown below

BigDecimal price = new BigDecimal("134.65");
String decString = price.setScale(3).toPlainString();
System.out.println(" Formatted price  ==> "+decString);

And output is

Formatted price ==> 134.650

There are one more way which will return with your local currency, but I think Brazil local currency is not supported with ;

       String formated= NumberFormat.getCurrencyInstance().format(price);
//String formated= NumberFormat.getCurrencyInstance(Locale.FRANCE).format(price);
       System.out.println(" Formatted price  with local   ==> "+formated);

[update] : to adapt, you can replace below one liner code with your parsing code

  String formatted=new BigDecimal(cleanString).setScale(3).toPlainString();
Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46