2

Hello i'm writing this program to get user input and format to 0,000.00 for example the program works fine when i try 5,768.80 BUT it doesn't when i try 5,768.08 as you can see the problem is that i can't put a 0 in the cents place before any other number... here is my code:

package com.calculadorabss.gorydev.calculadorabolivaressoberanos;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import java.text.DecimalFormat;
import java.text.ParseException;

class NumberTextWatcher implements TextWatcher {

    private DecimalFormat df;
    private DecimalFormat dfnd;
    private boolean hasFractionalPart;

    private EditText et;
    //Dar formato al texto de entrada separando por comas
    public NumberTextWatcher(EditText et)
    {
        df = new DecimalFormat("#,###,##");
        df.setDecimalSeparatorAlwaysShown(true);
        dfnd = new DecimalFormat("#,###,##");
        this.et = et;
        hasFractionalPart = false;
    }

    @SuppressWarnings("unused")
    private static final String TAG = "NumberTextWatcher";

    public void afterTextChanged(Editable s)
    {
        et.removeTextChangedListener(this);

        try {
            int inilen, endlen;
            inilen = et.getText().length();

            String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
            Number n = df.parse(v);
            int cp = et.getSelectionStart();
            if (hasFractionalPart) {
                et.setText(df.format(n));
            } else {
                et.setText(dfnd.format(n));
            }
            endlen = et.getText().length();
            int sel = (cp + (endlen - inilen));
            if (sel > 0 && sel <= et.getText().length()) {
                et.setSelection(sel);
            } else {
                // place cursor at the end?
                et.setSelection(et.getText().length() - 1);
            }
        } catch (NumberFormatException nfe) {
            // do nothing?
        } catch (ParseException e) {
            // do nothing?
        }

        et.addTextChangedListener(this);
    }
}

i want the program to be able to receive 0 as cents for example 67,789.05

Carlos Delgado
  • 159
  • 1
  • 2
  • 15

1 Answers1

1

Try this i think it work

public void afterTextChanged(Editable s)
    {
        et.removeTextChangedListener(this);

        try {
            int inilen, endlen;
            inilen = et.getText().length();

            String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
            hasFractionalPart = v.contains(".");
            Number n = df.parse(v);
            int cp = et.getSelectionStart();
            if (!hasFractionalPart) {
                et.setText(dfnd.format(n));
            }

            endlen = et.getText().length();
            int sel = (cp + (endlen - inilen));
            if (sel > 0 && sel <= et.getText().length()) {
                et.setSelection(sel);
            } else {
                // place cursor at the end?
                et.setSelection(et.getText().length() - 1);
            }
        } catch (NumberFormatException nfe) {
            // do nothing?
        } catch (ParseException e) {
            // do nothing?
        }

        et.addTextChangedListener(this);
    }
  • Hi! it works thanks but now i'm able to put more than 2 cents for example 150.988888 and i just want 150.98 or 150.05 thank you – Carlos Delgado Aug 19 '18 at 18:46
  • Add else part to remove double "." problem `if (!hasFractionalPart) { et.setText(dfnd.format(n)); }else { if (v.lastIndexOf(".")>v.indexOf(".")){ v = v.substring(0,v.length()-1); } } ` – Tejas Trivedi Aug 19 '18 at 19:05
  • Hello i added the else part but it's still doing the same the problem is not double "."the problem is that it is allowing more than 2 cents places and i just want 2 cent places... for example i just want it to allow 5,789,765.56 and not 5,789,765.5698 i hope you understand and thanks again – Carlos Delgado Aug 19 '18 at 20:06
  • Hello Tejas i have this problem with the format please take a look https://stackoverflow.com/q/51957784/4535062 thanks again – Carlos Delgado Aug 21 '18 at 23:01