Muhammad Umar Muhammad Umar - 2 years ago 153
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);


Answer Source

You can simply use DecimalFormat

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

You will get result in EditText as 22.12

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download