just_user
just_user

Reputation: 12057

trying to calculate BMI but get a 0.0 answer after using Math.round()

So I'm trying to calculate my bmi using the following method.

double bmi = weight / ((height * 100) * (height * 100));
bmi = Math.round(bmi * 100.0) / 100.0;

From the first line I get an answer that looks like this:

2.3457310760477412E-7

which is why I want to round this to one or two decimals. But this results in bmi being 0.0 instead.

I have also tried decimalformat which also returns 0. What am I doing wrong?

EDIT - SOLUTION

The problem was my formula! Centimeter should be divided by 10 not multiplied! Thanks!

//André

Upvotes: 0

Views: 1141

Answers (2)

chrisburke.io
chrisburke.io

Reputation: 1507

double weight = 89.0;
double height = 169.0;
double bmi = (weight / ((height * height) / 100)) * 100;

To clarify, weight and height are metric (KG/CM) - you need to convert the height in CM to meters (so you divide by 100) then the output is multiplied by 100 to give the BMI.

Upvotes: 0

kostja
kostja

Reputation: 61568

The number you are getting from your first line, 2.3457310760477412E-7 is already kinda small. It is roughly equal to 2.345/10000000 or 0.00000023457. And this is very close to zero. Math works as expected, rounding the number to the closest integer - 0.

The error is either in the input variables or in the fomula you are using. Maybe you have your height given in centimeters instead of meters?

Upvotes: 2

Related Questions