0

Here is a code that i want to make a bmi application.. So i want to make the result display like this 20.30.. How to make the result into 2 decimals places only.. Hope anybody cant help me at this part....

package com.example.bmi;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener {

    EditText displayweight;
    EditText displayheight;
    EditText displayresult;
    Button Calculate;
    Button Reset;
    double bmi=0;
    double valueheight=0;
    double valueweight=0;
    String result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initControls();
        /*TextView tv = new TextView(this);
        tv.setText("BMI Calculator");
        setContentView(tv);*/
    }

    private void initControls() {
        displayheight = (EditText)findViewById(R.id.displayheight);
        displayweight = (EditText)findViewById(R.id.displayweight);
        displayresult = (EditText)findViewById(R.id.displayresult);
        Calculate = (Button)findViewById(R.id.calculate);
        Reset = (Button)findViewById(R.id.reset);
        Calculate.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ calculate(); }});
        Reset.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ reset(); }

        private void reset() {
            displayheight.setText("");
            displayweight.setText("");
            displayresult.setText("");


        }});
    }

        private void calculate() {
            valueheight = Double.parseDouble(displayheight.getText().toString());
            valueweight = Double.parseDouble(displayweight.getText().toString());
            Double valueheightmeters;

            valueheightmeters = valueheight / 100;
            bmi = (valueweight / (valueheightmeters * valueheightmeters));

            //txttipamount.setText(Double.toString(bmi));

            if(bmi >= 30)
            {
                result = "Your BMI of " + Double.toString(bmi) + " is OBESE.";
                displayresult.setText(result);
            }
            else if (bmi >= 25)
            {
                result = "Your BMI of " + Double.toString(bmi) + " is OVERWEIGHT.";
                displayresult.setText(result);
            }
            else if (bmi >= 18.5)
            {
                result = "Your BMI of " + Double.toString(bmi) + " is IDEAL.";
                displayresult.setText(result);
            }
            else 
            {
                result = "Your BMI of " + Double.toString(bmi) + " is UNDERWEIGHT.";
                displayresult.setText(result);
            }

    }

        @Override
        public void onClick(View arg0) {
            switch (arg0.getId()){
                case R.id.reset:
                    displayresult.setText("");
                    break;
                    default:
                        break;

            }

        }
}

1 Answers1

0

this topic is discussed very often. Take a look at Round a double to 2 decimal places and see if it helps you.

Just let me know if you have any further questions.

Community
  • 1
  • 1
Dr. Dreave
  • 539
  • 5
  • 13