title: Android - How to only allow a certain number of decimal places
body: <p>Do you know of any method to make sure users can only enter figures with a maximum number of decimals.
  I'm not sure how to address this problem. In the MS SQL database I'm going to send data from my app I've got columns with this type <code>decimal(8,3)</code>
  Now considering the data type of the column that's finally going to store the value I want to validate in Android, I've considered these two cases: </p>
  
  <ol>
  <li>If the user enters a number with no decimals, the maximum number of digits must be 8</li>
  <li>If the user enters a number with decimals, the maximum number of digits must be 8 (including the digits to the right of the decimal point)</li>
  </ol>
  
  <p>Now I'm sure about the first case, but not so much about the second. <strong>Is it right to keep the number of maximum digits fixed(for example, always 8)? Or should I consider allowing a maximum of 8 digits to the left and 3 to the right of the decimal point?</strong></p>
  
  <p>Either way this is what I've been trying in Android: </p>
  
  <pre><code>mQuantityEditText.addTextChangedListener(new TextWatcher() {
              @Override
              public void afterTextChanged(Editable s) {
                  String str = mQuantityEditText.getText().toString();
                  DecimalFormat format = (DecimalFormat) DecimalFormat
                          .getInstance();
                  DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
                  char sep = symbols.getDecimalSeparator();
  
                  int indexOFdec = str.indexOf(sep);
  
                  if (indexOFdec &gt;= 0) {
                      if (str.substring(indexOFdec, str.length() - 1).length() &gt; 3) {                     
                          s.replace(0, s.length(),
                                  str.substring(0, str.length() - 1));                        
                      }
                  }
              }
  
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count,
                      int after) {
  
              }
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before,
                      int count) {
  
              }
          });
  </code></pre>
  
  <p>Even though, the above code handles the maximum number of decimal places. It does not limit the total number of digits allowed in the EditText.</p>
  
  <p>Do you think you could help me improve my code so that it handles both the maximum number of decimal places and the total number of digits allowed in a EditText (considering both numbers to the left and to the right of the decimal point)</p>
  
  <h1>EDIT</h1>
  
  <p>Well, now I'm trying what João Sousa suggested and here's what I've tried:</p>
  
  <p>1) I defined a  class that implements InputFilter</p>
  
  <pre><code>public class NumberInputFilter implements InputFilter {
      private Pattern mPattern;   
  
      public NumberInputFilter(int precision, int scale) {        
          String pattern="^\\-?(\\d{0," + (precision-scale) + "}|\\d{0," + (precision-scale) + "}\\.\\d{0," + scale + "})$";
          this.mPattern=Pattern.compile(pattern);
  
      }
  
      @Override
      public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int destinationStart, int destinationEnd) {
           if (end &gt; start) {
               // adding: filter   
               // build the resulting text
               String destinationString = destination.toString();
               String resultingTxt = destinationString.substring(0, destinationStart) + source.subSequence(start, end) + destinationString.substring(destinationEnd);
               // return null to accept the input or empty to reject it
               return resultingTxt.matches(this.mPattern.toString()) ? null : "";
           }
           // removing: always accept
           return null;
      }
  
  }
  </code></pre>
  
  <p>2) Tried to use the class like this :</p>
  
  <pre><code>mQuantityEditText.setFilters(new InputFilter[] { new NumberInputFilter(8,3)} );
  </code></pre>