-3

I have built a BMI calculator. In the user input field, how do I limit the digits after decimal point? Here is my code:

package com.example.bmicalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public double roundTwoDecimals(double d){

        DecimalFormat roundOff = new DecimalFormat("#.##");
        return Double.valueOf(roundOff.format(d));
    }

    public void buttonClicked(View v){

        EditText editTextHeight = findViewById(R.id.userHeight);
        EditText editTextWeight = findViewById(R.id.userWeight);

        TextView textViewResult = findViewById(R.id.userBMI);

        double height = Double.parseDouble(editTextHeight.getText().toString());
        double weight = Double.parseDouble(editTextWeight.getText().toString());

        double BMI = weight / (height * height);

        double cBMI = roundTwoDecimals(BMI);

        textViewResult.setText(Double.toString(cBMI));

        editTextHeight.setText("");
        editTextWeight.setText("");

    }
}

Please don't mark it as a duplicate. I've already tried all methods which are there in the StackOverflow but none of them worked for me.

I have used the below method

    InputFilter filter = new InputFilter() {

        final int maxDigitsBeforeDecimalPoint = 4;
        final int maxDigitsAfterDecimalPoint = 1;

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dStart, int dEnd) {

            StringBuilder builder = new StringBuilder(dest);
            builder.replace(dStart, dEnd, source.subSequence(start, end).toString());

            if(!builder.toString().matches(
                    "(([1-9]{1})([0-9]{0,"+(maxDigitsBeforeDecimalPoint-1)+"})?)?(\\.[0-9]{0,"+maxDigitsAfterDecimalPoint+"})?")){

                if(source.length() == 0){
                    return dest.subSequence(dStart, dEnd);
                }
                return "";
            }
            return null;
        }
    };

and in editText

editTextHeight.setFilters(new InputFilter[]{filter});
editTextWeight.setFilters(new InputFilter[]{filter});
  • did you try that? https://stackoverflow.com/questions/5357455/limit-decimal-places-in-android-edittext – Md. Asaduzzaman Nov 17 '19 at 09:43
  • @Md.Asaduzzaman yea I've tried all the methods mentioned in that link before posting this question but no use. – ghost deathrider Nov 17 '19 at 13:36
  • "I've already tried all methods which are there in the StackOverflow but none of them worked for me." – Please [edit] your question to show us exactly which methods you've tried, and to demonstrate exactly how each of them didn't work. It's really not possible for you to have tried every single one of the hundreds of answers for this available here. If you don't show which you've tried, then it's very likely that you're going to have them repeated to you, which just wastes the time of everyone involved. – Mike M. Nov 18 '19 at 01:24
  • @MikeM, the method which is mentioned in this link [https://stackoverflow.com/questions/5357455/limit-decimal-places-in-android-edittext](https://stackoverflow.com/questions/5357455/limit-decimal-places-in-android-edittext). Beleive me I tried all, no error is shown, the editText will accept more than 2 decimmal digits in every method. – ghost deathrider Nov 18 '19 at 03:58

2 Answers2

1

You can use TextWatcher.afterTextChanged() to alter the entered text.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • I tried but after entering "TextWatcher." it's not showing any methods. Can you please provide me some code, like where it has to be used?. I'm new to android development and this is my first app, so. – ghost deathrider Nov 18 '19 at 15:15
  • TextWatcher is an interface you need to implement. Then you assign that implementation to your EditText via setTextWatcher(). See this tutorial for instance on how to use it: https://www.tutorialspoint.com/how-to-use-the-text-watcher-class-in-android – Ridcully Nov 19 '19 at 06:25
0

Try using this, simplest way to limit decimal points,

double BMI = weight / (height * height);
double cBMI = Double.parseDouble(String.format("%.02f", BMI));

"%.02f" will limit the double value into two decimal point, you can change that as per your need, like for limiting the double value into 3 decimal point you just have to change the parameter ino "%.03f"