Better way to Format Currency Input editText?
I tested your method, but it fails when I use great numbers... I created this:
private String current = "";
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)){
[your_edittext].removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[$,.]", "");
double parsed = Double.parseDouble(cleanString);
String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));
current = formatted;
[your_edittext].setText(formatted);
[your_edittext].setSelection(formatted.length());
[your_edittext].addTextChangedListener(this);
}
}
Kotlin variant:
private var current: String = ""
override fun onTextChanged(
s: CharSequence,
start: Int,
before: Int,
count: Int
) {
if (s.toString() != current) {
discount_amount_edit_text.removeTextChangedListener(this)
val cleanString: String = s.replace("""[$,.]""".toRegex(), "")
val parsed = cleanString.toDouble()
val formatted = NumberFormat.getCurrencyInstance().format((parsed / 100))
current = formatted
discount_amount_edit_text.setText(formatted)
discount_amount_edit_text.setSelection(formatted.length)
discount_amount_edit_text.addTextChangedListener(this)
}
}
Here is my custom CurrencyEditText
import android.content.Context;import android.graphics.Rect;import android.text.Editable;import android.text.InputFilter;import android.text.InputType;import android.text.TextWatcher;
import android.util.AttributeSet;import android.widget.EditText;import java.math.BigDecimal;import java.math.RoundingMode;
import java.text.DecimalFormat;import java.text.DecimalFormatSymbols;
import java.util.Locale;
/**
* Some note <br/>
* <li>Always use locale US instead of default to make DecimalFormat work well in all language</li>
*/
public class CurrencyEditText extends android.support.v7.widget.AppCompatEditText {
private static String prefix = "VND ";
private static final int MAX_LENGTH = 20;
private static final int MAX_DECIMAL = 3;
private CurrencyTextWatcher currencyTextWatcher = new CurrencyTextWatcher(this, prefix);
public CurrencyEditText(Context context) {
this(context, null);
}
public CurrencyEditText(Context context, AttributeSet attrs) {
this(context, attrs, android.support.v7.appcompat.R.attr.editTextStyle);
}
public CurrencyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
this.setHint(prefix);
this.setFilters(new InputFilter[] { new InputFilter.LengthFilter(MAX_LENGTH) });
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
this.addTextChangedListener(currencyTextWatcher);
} else {
this.removeTextChangedListener(currencyTextWatcher);
}
handleCaseCurrencyEmpty(focused);
}
/**
* When currency empty <br/>
* + When focus EditText, set the default text = prefix (ex: VND) <br/>
* + When EditText lose focus, set the default text = "", EditText will display hint (ex:VND)
*/
private void handleCaseCurrencyEmpty(boolean focused) {
if (focused) {
if (getText().toString().isEmpty()) {
setText(prefix);
}
} else {
if (getText().toString().equals(prefix)) {
setText("");
}
}
}
private static class CurrencyTextWatcher implements TextWatcher {
private final EditText editText;
private String previousCleanString;
private String prefix;
CurrencyTextWatcher(EditText editText, String prefix) {
this.editText = editText;
this.prefix = prefix;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// do nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// do nothing
}
@Override
public void afterTextChanged(Editable editable) {
String str = editable.toString();
if (str.length() < prefix.length()) {
editText.setText(prefix);
editText.setSelection(prefix.length());
return;
}
if (str.equals(prefix)) {
return;
}
// cleanString this the string which not contain prefix and ,
String cleanString = str.replace(prefix, "").replaceAll("[,]", "");
// for prevent afterTextChanged recursive call
if (cleanString.equals(previousCleanString) || cleanString.isEmpty()) {
return;
}
previousCleanString = cleanString;
String formattedString;
if (cleanString.contains(".")) {
formattedString = formatDecimal(cleanString);
} else {
formattedString = formatInteger(cleanString);
}
editText.removeTextChangedListener(this); // Remove listener
editText.setText(formattedString);
handleSelection();
editText.addTextChangedListener(this); // Add back the listener
}
private String formatInteger(String str) {
BigDecimal parsed = new BigDecimal(str);
DecimalFormat formatter =
new DecimalFormat(prefix + "#,###", new DecimalFormatSymbols(Locale.US));
return formatter.format(parsed);
}
private String formatDecimal(String str) {
if (str.equals(".")) {
return prefix + ".";
}
BigDecimal parsed = new BigDecimal(str);
// example pattern VND #,###.00
DecimalFormat formatter = new DecimalFormat(prefix + "#,###." + getDecimalPattern(str),
new DecimalFormatSymbols(Locale.US));
formatter.setRoundingMode(RoundingMode.DOWN);
return formatter.format(parsed);
}
/**
* It will return suitable pattern for format decimal
* For example: 10.2 -> return 0 | 10.23 -> return 00, | 10.235 -> return 000
*/
private String getDecimalPattern(String str) {
int decimalCount = str.length() - str.indexOf(".") - 1;
StringBuilder decimalPattern = new StringBuilder();
for (int i = 0; i < decimalCount && i < MAX_DECIMAL; i++) {
decimalPattern.append("0");
}
return decimalPattern.toString();
}
private void handleSelection() {
if (editText.getText().length() <= MAX_LENGTH) {
editText.setSelection(editText.getText().length());
} else {
editText.setSelection(MAX_LENGTH);
}
}
}
}
Use it in XML like
<...CurrencyEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
You should edit 2 constant below for suitable for your project
private static String prefix = "VND ";
private static final int MAX_DECIMAL = 3;
Demo on github
Based on some of the above answers I created a MoneyTextWatcher which you'd use as follows:
priceEditText.addTextChangedListener(new MoneyTextWatcher(priceEditText));
and here's the class:
public class MoneyTextWatcher implements TextWatcher {
private final WeakReference<EditText> editTextWeakReference;
public MoneyTextWatcher(EditText editText) {
editTextWeakReference = new WeakReference<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 editable) {
EditText editText = editTextWeakReference.get();
if (editText == null) return;
String s = editable.toString();
if (s.isEmpty()) return;
editText.removeTextChangedListener(this);
String cleanString = s.replaceAll("[$,.]", "");
BigDecimal parsed = new BigDecimal(cleanString).setScale(2, BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100), BigDecimal.ROUND_FLOOR);
String formatted = NumberFormat.getCurrencyInstance().format(parsed);
editText.setText(formatted);
editText.setSelection(formatted.length());
editText.addTextChangedListener(this);
}
}