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,840
  • 9
  • 39
  • 52

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
  • 21,438
  • 5
  • 52
  • 61
-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
  • 105
  • 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