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é
The number you are getting from your first line,
2.3457310760477412E-7is already kinda small. It is roughly equal to2.345/10000000or0.00000023457. And this is very close to zero.Mathworks 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?