0

Please help me to view the results of this bmi calculations to two decimal points.

here is my code...

@Override
public void onClick(View v) {

    double weight;
    double height;
    double bmi;
    String msg = "";


    if (field_height.getText().toString().equals("") || field_weight.getText().toString().equals("")){

        Toast.makeText(getApplicationContext(), "No Valid Values!", Toast.LENGTH_LONG);

    }else {

        weight = Double.parseDouble(field_weight.getText().toString());
        height = Double.parseDouble(field_height.getText().toString());

        bmi = height * height;
        bmi = (weight / bmi);
    }
}
Pedro del Sol
  • 2,774
  • 9
  • 40
  • 49

2 Answers2

1

Try using String.format():

String bmiString = String.format( "%.2f", bmi);

Or use class DecimalFormat:

DecimalFormat df = new DecimalFormat("####0.00");
String bmiString = df.format(bmi);

Hope this will help~

Ferdous Ahamed
  • 19,328
  • 5
  • 45
  • 54
-1

[corrected]

Please find the answer below,

DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(bmi);

Hope, this will help.

Thanks

Govind Raj
  • 95
  • 6
  • 2
    Test code before submitting answers. 1. `DecimalFormat.format` expects a number, you are giving it a string, it's not going to work. 2. `0.00##` will display 4 decimals, OP wants 2. – BackSlash Apr 17 '17 at 09:06