Muhammad Umar Muhammad Umar - 1 year ago 106
Java Question

making EditText to show only two decimal places

i want to show only two decimal places in my edit text, ofc i wanna show currency in edit text but limiting its value to 2 digits after decimal.

I have seen some solutions using regular expression but i don't wanna do that. I HAVE been told that java support some internal library functions that can do that. Any one can please give me hints or give me some efficient code for that.



amount.addTextChangedListener(new TextWatcher()

public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
DecimalFormat formatVal = new DecimalFormat("##.##");
String formatted = formatVal.format(s);



You can simply use DecimalFormat

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

You will get result in EditText as 22.12