Limit Decimal Places in Android Edittext

Limit Decimal Places in Android EditText

This implementation of InputFilter solves the problem.

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.DigitsKeyListener;

public class MoneyValueFilter extends DigitsKeyListener {
public MoneyValueFilter() {
super(false, true);
}

private int digits = 2;

public void setDigits(int d) {
digits = d;
}

@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
CharSequence out = super.filter(source, start, end, dest, dstart, dend);

// if changed, replace the source
if (out != null) {
source = out;
start = 0;
end = out.length();
}

int len = end - start;

// if deleting, source is empty
// and deleting can't break anything
if (len == 0) {
return source;
}

int dlen = dest.length();

// Find the position of the decimal .
for (int i = 0; i < dstart; i++) {
if (dest.charAt(i) == '.') {
// being here means, that a number has
// been inserted after the dot
// check if the amount of digits is right
return (dlen-(i+1) + len > digits) ?
"" :
new SpannableStringBuilder(source, start, end);
}
}

for (int i = start; i < end; ++i) {
if (source.charAt(i) == '.') {
// being here means, dot has been inserted
// check if the amount of digits is right
if ((dlen-dend) + (end-(i + 1)) > digits)
return "";
else
break; // return new SpannableStringBuilder(source, start, end);
}
}

// if the dot is after the inserted part,
// nothing can break
return new SpannableStringBuilder(source, start, end);
}
}

Android edittext two decimal places

You should use InputFilter here is an example

public class DecimalDigitsInputFilter implements InputFilter {

Pattern mPattern;

public DecimalDigitsInputFilter(int digitsBeforeZero,int digitsAfterZero) {
mPattern=Pattern.compile("[0-9]{0," + (digitsBeforeZero-1) + "}+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?");
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

Matcher matcher=mPattern.matcher(dest);
if(!matcher.matches())
return "";
return null;
}

}

you can use it like this

editText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(5,2)});

How to make edittext in android always have 2 decimal digits regardless of the value entered by the user?

Set android:inputType="numberDecimal"
No predefined function to set the limit for the edittext to How many digit it should accept after the decimal point from user. We can achieve this by using TextWatcher. Please check the link given below

http://v4all123.blogspot.in/2013/05/set-limit-for-fraction-in-decimal.html

How to limit values after decimal point in editText in android?

You can use TextWatcher.afterTextChanged() to alter the entered text.

making EditText to show only two decimal places

You can simply use DecimalFormat

DecimalFormat format = new DecimalFormat("##.##");
String formatted = format.format(22.123);
editText.setText(formatted);

You will get result in EditText as 22.12

Android edittext - do not allow decimal separator alone

If this:

if(field1.getText().toString().equals(".")){field1.setText(0);}

is your exact code, then for sure it failed because 0 is not a string, it's an integer considered to be a resource id.

So first try this:

if(field1.getText().toString().equals(".")){field1.setText("0");}

If it fails again then consider that a NumberFormatException can be resolved by try/catch like this:

double value = 0.0;
try {
value = Double.parseDouble(field1.getText().toString());
} catch (NumberFormatException e) {
field1.setText("0");
e.printStackTrace();
}


Related Topics



Leave a reply



Submit