Results 1 to 1 of 1

Thread: Fat Gram Calculator

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Location
    Baltimore, MD
    Posts
    9

    Fat Gram Calculator

    I'm trying to do a project from a book, and I have no idea what it's telling me to calculate, other than it's a percentage.

    Here's what it states:

    One gram of fat has 9 calories, so:
    Calories from fat = fat grams * 9

    the percentage of calories from fat can be calculated as:
    Percentage of calories from fat = Calories from fat total calories

    This is what my professor wrote in my book when I asked him for help about understanding what it was asking for:

    cal_fat = fat_gr(txtbox2) * 9
    perc_fromfat= calfat (over) cal_total<--- (txtbox1)
    = cal_fat/cal_total

    Here's the code:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim FatTimes9 As Integer
            Dim FatPercent As Integer
            If Val(txtFat.Text) > Val(txtCalories.Text) Then
                MessageBox.Show("Calculation Error", "Calculation Error")
                txtTotal.Clear()
            End If
            If Val(txtCalories.Text) < 1 Then
                MessageBox.Show("Value not greater than 1", "Input Error")
                txtTotal.Clear()
            End If
            FatTimes9 = Val(txtFat.Text) * 9
            FatPercent = FatTimes9 / Val(txtCalories.Text)
            txtTotal.Text = FormatPercent(FatPercent)
        End Sub
    The examples in the book say:

    200 calories, 8 fat grams = 36%
    150 calories, 2 fat grams = 12%
    500 calories, 30 fat grams = 54%

    EDIT: Nevermind. I figured it out. Since I set the FatPercent dimension to an integer, instead of a single, it was rounding off to the nerest whole number, thus giving me a 0.
    Last edited by gir489; Apr 14th, 2008 at 04:42 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width