'How to remove $ mark and add € mark to number format in android studio
The belove code give me $00.00 format number and and i want €00,00 like this. WHat i really want is , when User type on EditeText it should like bigin €00,09 like this . And another problem is if i enter numbers and delete fastly the app crash. Any one know how to solve this?
public class MoneyTextWatcher implements TextWatcher {
private final WeakReference<EditText> editTextWeakReference;
public MoneyTextWatcher(EditText mEditText) {
editTextWeakReference = new WeakReference<EditText>(mEditText);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
EditText editTex = editTextWeakReference.get();
if(!s.toString().equals(editTex.getText())) {
editTex.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[€,.]", "");
double parsed = Double.parseDouble(cleanString.replaceAll("[^\\d]", ""));
String formatted = NumberFormat.getCurrencyInstance().format((parsed / 100));
editTex.setText(formatted);
editTex.setSelection(formatted.length());
editTex.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable s) {
}
}
}
Solution 1:[1]
So, I think the main question is how to format your double with a Euro sign. For this, you can simply set the currency NumberFormat should use:
NumberFormat format = NumberFormat.getCurrencyInstance();
Currency currency = Currency.getInstance("EUR");
format.setCurrency(currency);
System.out.println(format.format(40.27));
Output:
€40.27
This method only adds the € sign, but does not change the decimal point or anything else. Alternatively, you can also use the format of a specific country. For example France:
NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);
System.out.println(france.format(40.27));
Output:
40,27 €
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JANO |
