【问题标题】:Convert formatCurrency (rupiah) to int将 formatCurrency (印尼盾) 转换为 int
【发布时间】:2022-01-20 12:19:12
【问题描述】:

这里我有一个这样的方法:

// method currency format
    private String formatRupiah(Double number) {
        Locale locale = new Locale("IND", "ID");
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
        String formatRupiah = numberFormat.format(number);
        String[] split = formatRupiah.split(",");
        int length = split[0].length();
        String formatRupiahString = split[0].substring(0, 2) + " " + split[0].substring(2, length);
        return formatRupiahString;
    }

还有这个方法可以将编辑文本中的文本改成货币格式:

private void editTextToFormatCurrency() {
        etJumlah.addTextChangedListener(new TextWatcher() {

            private String jumlahFormat = Objects.requireNonNull(etJumlah.getText()).toString().trim();

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().equals(jumlahFormat)) {
                    etJumlah.removeTextChangedListener(this);
                    String replace = s.toString().replaceAll("[Rp. ]", "");
                    if (!replace.isEmpty()) {
                        jumlahFormat = formatRupiah(Double.parseDouble(replace));
                    } else {
                        jumlahFormat = "";
                    }
                    etJumlah.setText(jumlahFormat);
                    etJumlah.setSelection(jumlahFormat.length());
                    etJumlah.addTextChangedListener(this);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

现在我的问题是如何在没有格式货币的情况下更改 TextToFormatCurrencycto int 或 Integer ?
我改回 Integer 或 int 的目标是,我可以使用数据类型编号上传到 Firestore。

【问题讨论】:

    标签: java string integer


    【解决方案1】:

    答案是: https://www.youtube.com/watch?v=TdgQW8T-KeQ&t(这是我的视频)
    https://www.youtube.com/watch?v=NQOK2cam3js&t(这是我的灵感视频)

    String jumlahString = Objects.requireNonNull(String.valueOf(layoutJumlah.getEditText().getText()));
    jumlahString = jumlahString.replaceAll("[^0-9]", "");
    jumlah = Integer.parseInt(jumlahString);
    

    【讨论】:

    • 请不要误用答案来宣传您的视频;而是用文字写出答案。您可能参考视频以获取更多信息,但不要将其用作主要答案。
    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2020-07-03
    • 2012-01-07
    • 1970-01-01
    • 2018-04-28
    • 2011-05-13
    相关资源
    最近更新 更多