2
import java.util.Scanner;
public class bmi {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        float height,heightSquared,BMI;

        int weight;

        System.out.println("Your height in meters:");

        height = scan.nextFloat();

        System.out.println("Your weight in kilograms:");

        weight = scan.nextInt();

        heightSquared = (height*height);

        BMI = weight/heightSquared;

        if (BMI < 18.5) {
            System.out.println("Your BMI result:"+ BMI + "(UNDER-WEIGHT)");
        }
        else if (BMI > 18.4 && BMI < 24.6) {
            System.out.println("Your BMI result:"+ BMI + "(NORMAL-WEIGHT)");
        }
        else if (BMI > 24.9 && BMI < 30) {
            System.out.println("Your BMI result:"+ BMI + "(OVER-WEIGHT)");
        }
        else {

            System.out.println("Your BMI result:"+ BMI + "(OBESSE)");
        }

    }
}

This is my code for getting the body mass index (BMI) of a person it's okay when running but my only problem is how do i turn the answer in 2 decimals? for example:

Your height in meters: 1.70 Your weight in kilograms: 50 Your BMI result:17.301032(UNDER-WEIGHT)

i want the output to be only 17.30 or in 2 decimals. what should i change?

1 Answers1

0

Try with this. You can use %2f to get two decimal places

Your BMI result:"+ String.format("%.2f", BMI)
John Joe
  • 10,340
  • 9
  • 48
  • 104