Only need two decimal places in my output

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.";
        }
    }
}

}

Upvotes: 0

Views: 2077

Answers (4)

abo
abo

Reputation: 378

In calculateBMI() do the following.

NumberFormat numberFormat = NumberFormat.getIntegerInstance();
numberFormat.setMaximumFractionDigits(2);
numberFormat.format(x);

Where x will be your calculated double value.

Upvotes: 0

widavies
widavies

Reputation: 986

Here's a handy method I use:

public static double round (double value, int precision) {
    int scale = (int) Math.pow(10, precision);
    return (double) Math.round(value * scale) / scale;
}

Just pass in your value, then pass in precision (the number of decimals to display).

For example: round(2.3891237, 2) will return 2.39

Upvotes: 0

DevilsHnd - 退した
DevilsHnd - 退した

Reputation: 9202

It's always best to maintain precision when dealing with numbers but there are those certain situations where in depth precision is not really a main concern. If you want to set a specific double data type variable to a specific decimal precision then you could pass your Double type value to a method like this:

double bmi = round(23.053626543209877, 2);
System.out.println("BMI = " + bmi);

private static double round(double value, int precision) {
    int scale = (int) Math.pow(10, precision);
    return (double) Math.round(value * scale) / scale;
}

Output will be:

BMI = 23.05

As the method name suggests, the returned result is also rounded.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201537

You could use formatted io with printf, like

double bmi = 23.053626543209877;
System.out.printf("BMI = %.2f%n", bmi);

Outputs (as I believe you wanted, and will even perform rounding)

BMI = 23.05

Upvotes: 1

Related Questions