将数字转换为货币格式

将数字转换为货币格式

本文介绍了文本更改时将数字转换为货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 RxTextView.textChanges 用于 EditText ,当用户键入更改值<$ c $时c> EditText 来将数字转换为货币格式,如下所示:

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

但是我看不到任何将数字转换为货币格式

我使用的是: 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() {

            }
        });


推荐答案

使用此TextWatcher类:

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

这篇关于文本更改时将数字转换为货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 12:13