I am a beginner in android.I have a edittext in my activity.I need to make the edittext entries to two digit floating numbers after user enter the data.For example if the user enters 100 then after completion it should display as 100.00, if user enters 450.468 it should display as 450.47 in the edittext.How to do this? Please help me.
Asked
Active
Viewed 2,546 times
-1
-
What's the flow of the application you are creating? Is it just an edittext where the user enters a number and hits a submit button? In this case you can just use the Math.round function like this (double roundOff = (double) Math.round(a * 100) / 100;) – Mathieu Brouwers Apr 09 '15 at 06:53
5 Answers
2
You can use Ravi's answer along with TextChangeListener on edittext as
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
DecimalFormat form = new DecimalFormat("0.00");
String FormattedText=form.format(UserEnteredValue);
textView.setText(FormattedText);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}});

Narendra
- 609
- 1
- 12
- 28
-
-
1No its not string you have to type cast it to double, you can do something like String FormattedText=form.format(Double.parseDouble(UserEnteredValue)); – Narendra Apr 09 '15 at 09:13
1
try this
String seq=editTextt.getText();
double d=Double.parseDouble(seq);
NumberFormat formatter = NumberFormat.getCurrencyInstance();
editTextt.setText(""+formatter.format(d));
System.out.println(formatter.format(d));

Karthika PB
- 1,373
- 9
- 16
0
You can try below code -
DecimalFormat form = new DecimalFormat("0.00");
String FormattedText=form.format(UserEnteredValue);
textView.setText(FormattedText);
hope it helps you.

Ravi Bhandari
- 4,682
- 8
- 40
- 68
0
I think in that case you have to use InputFilter looks into the below solution that will same as your problem.
How to limit EditText length to 7 integers and 2 decimal places?
Limit Decimal Places in Android EditText
hope it helps you.

Community
- 1
- 1

Born To Win
- 3,319
- 3
- 19
- 27
0
Try this:
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
DecimalFormat form = new DecimalFormat("0.00");
String FormattedText=form.format(UserEnteredValue);
textView.setText(FormattedText);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}});

Magnilex
- 11,584
- 9
- 62
- 84

Vinay Sagar Savithru
- 19
- 6
-
Could you also explain your answer? Answers are always stronger if they include some kind of explanation. – Magnilex Apr 09 '15 at 07:40