Homework Help

C

CSharp-Jay

So I am working on my college homework for our C# class, and am kind
of stuck on something that I could use some help with. The assignment
is to create a BMI Calculator. The test data the instructor wants us
to use is a height of 63 and a weight of 140, which should output as
24.8. I've got everything down except one problem, I think when I
convert my double bmi variable to string for output, it truncates the
value and comes out as just 24, instead of 24.8. How do I set up the
formatting for my output? My code example is below, any help is
greatly appreciated, thank you :)

private void btnCalculateBMI_Click(object sender, EventArgs e)
{
// Declare and assign variables used for BMI Calculation.
int height, weight = 0;
double bmi = 0;

// Convert string inputs to our declared variables.
height = int.Parse(txtInputHeight.Text);
weight = int.Parse(txtInputWeight.Text);

// Perform the BMI calculation and assign it to a
variable.
bmi = 703 * weight / (height * height);
// Round off the bmi variable
Math.Round(bmi);

// Output the BMI calculation to our output label
lblOutputBMI.Text = Convert.ToString(bmi);
}
 
J

Jeroen Mostert

CSharp-Jay said:
So I am working on my college homework for our C# class, and am kind
of stuck on something that I could use some help with. The assignment
is to create a BMI Calculator. The test data the instructor wants us
to use is a height of 63 and a weight of 140, which should output as
24.8. I've got everything down except one problem, I think when I
convert my double bmi variable to string for output, it truncates the
value and comes out as just 24, instead of 24.8. How do I set up the
formatting for my output? My code example is below, any help is
greatly appreciated, thank you :)

private void btnCalculateBMI_Click(object sender, EventArgs e)
{
// Declare and assign variables used for BMI Calculation.
int height, weight = 0;
double bmi = 0;

// Convert string inputs to our declared variables.
height = int.Parse(txtInputHeight.Text);
weight = int.Parse(txtInputWeight.Text);

// Perform the BMI calculation and assign it to a
variable.
bmi = 703 * weight / (height * height);

Dividing two integers will use integer division. Declare both "height" and
"weight" as doubles too and the problem disappears. Alternatively, use
"703.0" (which makes it a floating-point constant rather than an integer
constant). Declaring "height" and "width" as floating-point to begin with is
better, though, because it avoids overflow. A multiplication like "height *
height" will fail when height exceeds 46341, as the result will not fit in
an integer -- not a problem in this particular application, but still.
// Round off the bmi variable
Math.Round(bmi);
Math.Round() returns a value, it does not modify its argument. This
particular statement does nothing. If you want to round, you have to assign
the result:

bmi = Math.Round(bmi);

However, this will round the number to a whole value leaving no decimal
places, which is not what you want. You should remove this statement altogether.
// Output the BMI calculation to our output label
lblOutputBMI.Text = Convert.ToString(bmi);

To format the value with one decimal, use bmi.ToString("F1"). This will take
care of rounding too.
 
C

CSharp-Jay

Dividing two integers will use integer division. Declare both "height" and
"weight" as doubles too and the problem disappears. Alternatively, use
"703.0" (which makes it a floating-point constant rather than an integer
constant). Declaring "height" and "width" as floating-point to begin withis
better, though, because it avoids overflow. A multiplication like "height*
height" will fail when height exceeds 46341, as the result will not fit in
an integer -- not a problem in this particular application, but still.


Math.Round() returns a value, it does not modify its argument. This
particular statement does nothing. If you want to round, you have to assign
the result:

   bmi = Math.Round(bmi);

However, this will round the number to a whole value leaving no decimal
places, which is not what you want. You should remove this statement altogether.


To format the value with one decimal, use bmi.ToString("F1"). This will take
care of rounding too.

Awesome all of that worked, thanks so much! So whats the deal with
integer division then? Is it correct to assume that if you expect
your answer to be a decimal, that you must use a floating point
variable type for the variables in your equation? And if you expect
your answer to be a whole number, using integers in your equation is
the way to go? Thanks so much for the help :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top