【发布时间】: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。
【问题讨论】: