I am very new to visual basic 2010 and am really struggling with it. My assignment is a fat gram calculator and the example is there are 200 calories in the food and there are 8 grams of fat in the food. The calculator gives the percentage of calories that come from fat. I have the equation and the code written but when I calculate I get .36% instead of 36.00%. Any help is appreciated.

Here is my code:

Private Sub Button1_Click( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles Button1.Click
        Dim decCalories As Decimal
        Dim decFat As Decimal
        Dim decPercent As Decimal
        Dim decX As Decimal

        lblPercent.Visible = True
        decCalories = CDec(txtCalories.Text)
        decFat = CDec(txtFat.Text)

        decX =  decFat * 9
        decPercent = decX / decCalories

        If decPercent < 30 then 

          lblError.Text = "Low Fat Food"

        End If

        If decFat > decCalories then    
                lblError.Text = "Calorie or Fat Grams Entered Incorrectly"
        End If

        lblPercent.Text = decPercent.ToString("n2") & "%"
        lblError.Text = "Please try again.  Enter a number"
End Try
End Sub
3 Years
Discussion Span
Last Post by swilcox1110

to get 36.00% , you need to change the code at line 13 with this:

decPercent = (decX / decCalories)*100

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.