Search code examples
androidrx-androidandroid-textwatcher

converting numbers to currency format when text changes


I am using RxTextView.textChanges for EditText that when the user is typing change value of EditText to convert numbers to currency format like below:

1,000

But I can't see any convert numbers to currency format.

I am using from: NumberFormat.getNumberInstance(Locale.US).format(productPrice);

My code is like bellow:

Observable<CharSequence> observableDiscountPrice = RxTextView.textChanges(discountPriceEdittext);
        observableDiscountPrice.map(new Function<CharSequence, Boolean>() {
            @Override
            public Boolean apply(@io.reactivex.annotations.NonNull CharSequence charSequence) throws Exception {
                try {
                    if (charSequence.length() > 0) {
                        String pPrice = NumberFormat.getNumberInstance(Locale.US).format(charSequence.toString());
                        originalPriceEdittext.setText(String.valueOf(pPrice));
                        return true;
                    } else {
                        return false;
                    }
                } catch (Exception e) {
                    return true;
                }
            }
        }).subscribe(new Subject<Boolean>() {
            @Override
            public boolean hasObservers() {
                return false;
            }

            @Override
            public boolean hasThrowable() {
                return false;
            }

            @Override
            public boolean hasComplete() {
                return false;
            }

            @Override
            public Throwable getThrowable() {
                return null;
            }

            @Override
            protected void subscribeActual(Observer<? super Boolean> observer) {

            }

            @Override
            public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {

            }

            @Override
            public void onNext(@io.reactivex.annotations.NonNull Boolean aBoolean) {
            }

            @Override
            public void onError(@io.reactivex.annotations.NonNull Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

Solution

  • Use this TextWatcher class:

    public class NumberTextWatcherWithSeperator implements TextWatcher {
    
    private EditText editText;
    
    
    public NumberTextWatcherWithSeperator(EditText editText) {
        this.editText = editText;
    
    
    }
    
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
    }
    
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    
    }
    
    @Override
    public void afterTextChanged(Editable s) {
        try {
            editText.removeTextChangedListener(this);
            String value = editText.getText().toString();
    
            if (!value.equals("")) {
    
                if (value.startsWith(".")) {
                    editText.setText("0.");
                }
                if (value.startsWith("0") && !value.startsWith("0.")) {
                    editText.setText("");
    
                }
    
                String str = editText.getText().toString().replaceAll(",", "");
                if (!value.equals(""))
                    editText.setText(getDecimalFormattedString(str));
                editText.setSelection(editText.getText().toString().length());
            }
            editText.addTextChangedListener(this);
        } catch (Exception ex) {
            ex.printStackTrace();
            editText.addTextChangedListener(this);
        }
    }
    
    private static String getDecimalFormattedString(String value) {
        StringTokenizer lst = new StringTokenizer(value, ".");
        String str1 = value;
        String str2 = "";
        if (lst.countTokens() > 1) {
            str1 = lst.nextToken();
            str2 = lst.nextToken();
        }
        String str3 = "";
        int i = 0;
        int j = -1 + str1.length();
        if (str1.charAt(-1 + str1.length()) == '.') {
            j--;
            str3 = ".";
        }
        for (int k = j; ; k--) {
            if (k < 0) {
                if (str2.length() > 0)
                    str3 = str3 + "." + str2;
                return str3;
            }
            if (i == 3) {
                str3 = "," + str3;
                i = 0;
            }
            str3 = str1.charAt(k) + str3;
            i++;
        }
    
    }
    
    }
    

    and

    yourEditText.addTextChangedListener(new NumberTextWatcherWithSeperator(yourEditText));