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
Try
        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") & "%"
Catch
        lblError.Text = "Please try again.  Enter a number"
End Try
End Sub

Recommended Answers

All 2 Replies

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

decPercent = (decX / decCalories)*100

Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.