-->

converting numbers to currency format when text ch

2019-08-23 11:53发布

问题:

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() {

            }
        });

回答1:

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));


回答2:

Take a look at https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

    DecimalFormat myFormatter = new DecimalFormat("###,###.###");
    String output = myFormatter.format(156456.673);
    System.out.println(156456.673 + " " + "###,###.###" + " " + output);

// I/System.out: 156456.673 ###,###.### 156,456.673


回答3:

I'm using this watcher to do the same thing:

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

    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    import java.util.Locale;

    public class CurrencyTextWatcher implements TextWatcher {

    private EditText ed;
    private String lastText;
    private boolean bDel = false;
    private boolean bInsert = false;
    private int pos;

    public CurrencyTextWatcher(EditText ed) {
        this.ed = ed;
    }

    public static String getStringWithSeparator(long value) {
        DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
        String f = formatter.format(value);
        return f;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        bDel = false;
        bInsert = false;
        if (before == 1 && count == 0) {
            bDel = true;
            pos = start;
        } else if (before == 0 && count == 1) {
            bInsert = true;
            pos = start;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        lastText = s.toString();
    }

    @Override
    public void afterTextChanged(Editable s) {
        ed.removeTextChangedListener(this);
        StringBuilder sb = new StringBuilder();
        String text = s.toString();
        for (int i = 0; i < text.length(); i++) {
            if ((text.charAt(i) >= 0x30 && text.charAt(i) <= 0x39) || text.charAt(i) == '.' || text.charAt(i) == ',')
                sb.append(text.charAt(i));
        }
        if (!sb.toString().equals(s.toString())) {
            bDel = bInsert = false;
        }
        String newText = getFormattedString(sb.toString());
        s.clear();
        s.append(newText);
        ed.addTextChangedListener(this);

        if (bDel) {
            int idx = pos;
            if (lastText.length() - 1 > newText.length())
                idx--; 
            if (idx < 0)
                idx = 0;
            ed.setSelection(idx);
        } else if (bInsert) {
            int idx = pos + 1;
            if (lastText.length() + 1 < newText.length())
                idx++; 
            if (idx > newText.length())
                idx = newText.length();
            ed.setSelection(idx);
        }
    }

    private String getFormattedString(String text) {
        String res = "";
        try {
            String temp = text.replace(",", "");
            long part1;
            String part2 = "";
            int dotIndex = temp.indexOf(".");
            if (dotIndex >= 0) {
                part1 = Long.parseLong(temp.substring(0, dotIndex));
                if (dotIndex + 1 <= temp.length()) {
                    part2 = temp.substring(dotIndex + 1).trim().replace(".", "").replace(",", "");
                }
            } else
                part1 = Long.parseLong(temp);

            res = getStringWithSeparator(part1);
            if (part2.length() > 0)
                res += "." + part2;
            else if (dotIndex >= 0)
                res += ".";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return res;
    }
}


回答4:

In case you want to and a dollar sign

    fun multiply_two_numbers() {

    val x = etValOne.text.toString()
    val y = etValTwo.text.toString()
    val c =  x.toDouble().times(y.toDouble())
    //val c = (x.toDouble() * y.toDouble())
    val df = DecimalFormat("$ "+"0.00")
    df.roundingMode = RoundingMode.CEILING
    df.format(c)
    etANS.setText(df.format(c))
}