Public Class Form1
    
    Private Sub btnCompute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        Dim percent, calories As Double
        Dim grams As Double
        Dim allow_amount As Double = 30

        grams = txtGrams.Text
        calories = txtCalories.Text

        percent = (grams * 9) / calories * 100

        Dim fmtstr As String = "{0, 15}"
        lstResult.Items.Clear()
        lstResult.Items.Add(String.Format(fmtstr, txtFood.Text & " containts " & percent & _
"% calories from fat."))
        If percent > allow_amount Then
            lstResult.Items.Add(String.Format(fmtstr, txtFood.Text & " exceed the AHA recommendation."))
        End If
    End Sub
End Class

----------------------------------------------------------------------------
I would like the "Percent" result to come out as 37.50%

Public Class Form1
    
    Private Sub btnCompute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCompute.Click
        Dim percent, calories As Double
        Dim grams As Double
        Dim allow_amount As Double = 30

        grams = txtGrams.Text
        calories = txtCalories.Text

        percent = (grams * 9) / calories * 100

        Dim fmtstr As String = "{0, 15}"
        lstResult.Items.Clear()
        lstResult.Items.Add(String.Format(fmtstr, txtFood.Text & " containts " & percent & _
"% calories from fat."))
        If percent > allow_amount Then
            lstResult.Items.Add(String.Format(fmtstr, txtFood.Text & " exceed the AHA recommendation."))
        End If
    End Sub
End Class

----------------------------------------------------------------------------
I would like the "Percent" result to come out as 37.50%

I have to say try FormatPercent
I also inserted a Try Catch Block to stop non-entry into form

Let me know how it turns out.


Private Sub btnCompute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCompute.Click
Dim percent, calories, food, grams As Double

Dim allow_amount As Double = 30
Try
grams = txtGrams.Text
calories = txtCalories.Text
food = txtFood.Text

percent = FormatPercent(grams * 9) / calories * 100
Catch
MessageBox.Show("You must enter a value", "Invalid Entry")

End Try
Dim fmtstr As String = "{0, 15}"
lstResult.Items.Clear()
lstResult.Items.Add(String.Format(fmtstr, txtFood.Text & " containts " & percent & _
"% calories from fat."))
If percent > allow_amount Then
lstResult.Items.Add(String.Format(fmtstr, txtFood.Text & " exceed the AHA recommendation."))
End If
End Sub

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.