0
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%

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by puppykillaz
0
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

This topic has been dead for over six months. 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.