title: How to format number in android EditText after realtime editing
body: <p>I have an EditText in which the user should input a number including decimals and i want a thousand separator automatically added onto the input number I tried a couple of other methods but some do not allow floating point numbers so i came up with this code which works well only that the string input is not being edited in realtime to one with possible thousand separators and the errors seem to stem from the s.replace();</p>
  
  <pre><code>    am2 = new TextWatcher(){
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      }
      public void onTextChanged(CharSequence s, int start, int before, int count) {}
      public void afterTextChanged(Editable s) {
          if (s.toString().equals("")) {
              amount.setText("");
              value = 0;
          }else{
              StringBuffer strBuff = new StringBuffer();
              char c;
              for (int i = 0; i &lt; amount2.getText().toString().length() ; i++) {
                  c = amount2.getText().toString().charAt(i);
                  if (Character.isDigit(c)) {
                      strBuff.append(c);
                  }
              }
              value = Double.parseDouble(strBuff.toString());
              reverse();
              NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
              ((DecimalFormat)nf2).applyPattern("###,###.#######");
              s.replace(0, s.length(), nf2.format(value));
          }
      }
  };
  </code></pre>