How to get the double value that is only two digit after decimal point. The output I receive gives me BMI = 23.053626543209877 if my height input is 72 and weight 170. I'm not sure how to get rid of the trailing numbers after .05
Here is the code I have:
import java.util.Scanner;
public class bmi {
private static Scanner scanner;
public static void main(String[] args) {
// Variables
double height;
double weight;
double bmi;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your height: ");
height = keyboard.nextDouble();
if(height<=0)
{
System.out.println("That's an invalid height.");
return;
}
System.out.println("Please enter your weight: ");
weight = keyboard.nextDouble();
if(weight<=0)
{
System.out.println("That's an invalid weight.");
return;
}
bmi = calculateBMI(height, weight);
System.out.println("BMI = " + bmi);
System.out.println(bmiDescription(bmi));
keyboard.close();
}
static double calculateBMI (double height, double weight) {
return weight * 703 / (height * height);
}
static String bmiDescription (double bmi) {
if (bmi < 18.5) {
return "You are underweight.";
} else {
if (bmi > 25) {
return "You are overweight.";
} else {
return "You are optimal.";
}
}
}
}