Chintan Soni
Chintan Soni

Reputation: 25287

Format numbers with some pattern

I am looking for some typical implementation as below:

I need my Price EditText Field always take values from behind, like:

Initially, EditText is having value 0 by default,

Now when I start typing, say when I press 1 from number keypad,

it should result in printing 0.01.

Next, when I press 2,

it should print as 0.12

Next, I press 3

it should print as 1.23 ... and so on..

Maximum it should not be able to type value of more than 9999.99..

Is there anyone with good with formatting skills ?

Upvotes: 0

Views: 64

Answers (1)

Yash Soni
Yash Soni

Reputation: 456

Assuming you need precision upto 2 digits after decimal:

public void afterTextChanged(Editable s) {
    // handle empty editText value and zero
  double etValue = Double.valueOf(editText.getText().toString());
  String input = s.toString();
  int len = input.length();
    for(int i=0; i<len; i++){
        etValue *= 10;
        etValue += Double.valueOf("0.0"+input.charAt(i));
    }
  editText.setText(String.valueOf(etValue));
}

Upvotes: 1

Related Questions