Skip to content
Advertisement

Casting a string to an integer and then to a double – how to retrieve up to four positions behind the decimal position in a correct way?

I am currently working on a simple BMI (Body Mass Index) calculator in Java, in a larger Java / Spring Boot application with Mojito Tests. My function uses height and weight as Input values to calculate the BMI. Through the arithmetic operations, the received Input values – Strings, casted to Integers – result in a number that represents the BMI; however, the BMI has more than four positions behind the Decimal; that is why I decided to cast the result of the arithmetic operation of height and weight into a Double value.

For some reason I don’t understand, casting the Input values for height and weight – Strings – to Doubles results in failing Mojito tests in completely different places – not related to the actual function and test -, as the parsed values are now used in other places as Double values and hence result in failing tests; hence, I decided to cast the Input values for height and weight from String to Integer. The problem then is that this results in a BMI that is a number with just one decimal position – f.e. 0.0, where as I had the double value before like e.g. 0.0034 or 0.0040, which are correct BMI values just four digits behind the decimal.

In short: I cannot cast String to Double as this will result in failing tests for some reason I don’t understand in other places of the application.

Casting the result of the BMI calculation results in an Integer value with the missing positions behind the decimal in order to make the function work.

As I am stuck here, I would appreciate any hints or help, thank you in advance!

This is my function:

 public boolean calculateBMI() {
    int totalWeight = Integer.parseInt(gewicht);
    int totalHeight = Integer.parseInt(koerpergroesse);
    // calculate bmi
    float bmi = totalWeight / (totalHeight * totalHeight);

    if (bmi > 0.0030 && bmi <= 0.0040) {
      return true;
    }
    return false;
  }
}

When I enter 183 for height and 120 for weight, I receive e.g. a BMI Value of 0.0; as a Double value, it would be something like 0.0034 and the function would return the correct boolean value.

Advertisement

Answer

Here is sample code that reproduces the issue you’re observing:

int totalWeight = 120;
int totalHeight = 183;
float bmi = totalWeight / (totalHeight * totalHeight);
System.out.println("bmi: " + bmi);

This code assigns “0.0” to bmi instead of a fractional value like you’re expecting:

bmi: 0.0

With that code, IntelliJ shows the following warning text which is informative for what’s going on. It doesn’t tell you how to fix the issue, but does give you a starting point for researching next steps.

'totalWeight / (totalHeight * totalHeight)': integer division in floating-point context
Result of 'totalWeight / (totalHeight * totalHeight)' is always '0'

There are several possible fixes, including to cast any of the operands as float to match the left-hand type definition for bmi.

So you could do this:

float bmi = (float) totalWeight / (totalHeight * totalHeight);

or this:

float bmi = totalWeight / ((float) totalHeight * totalHeight);

or this:

float bmi = totalWeight / (totalHeight * (float)totalHeight);

and each of those would produce the same output:

bmi: 0.0035832662
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement