Visual Basic format functions

knowing that:

Format Percent (0.185, 2) will out put 18.50%

if i have the following partial code:

Dim food, calories, grams As String
Dim Fact_Calories, Answer As Double

Fact_Calories = grams * 9
        Answer = Fact_Calories / calories

how can I format this Answer variable to the percent fortmat mention above
LVL 1
jsctechyAsked:
Who is Participating?
 
bobbit31Connect With a Mentor Commented:
Sub Command1_Click()
  msgbox   Format(cal, "00.00%")
end sub

Function cal() As Double
        Fact_Calories = grams * 9
        Answer = Fact_Calories / calories * 100
End Function
0
 
bobbit31Commented:
Format(Answer*100, "#0.00%")
0
 
jsctechyAuthor Commented:
it prints 0.00% and I want 00.00%, meaning 12.00%
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
bobbit31Commented:
Format(Answer*100, "00.00%")

;)
0
 
jsctechyAuthor Commented:
for some reason if the result is 35.70% it prints 35.7% but i need the zero also in the out put

Thanks
0
 
bramsquadCommented:
it shouldnt

MsgBox(Format(0.357, "00.00%"))

returns this

35.70%

0
 
jsctechyAuthor Commented:
knowing the number yes but using the variable will print out 35.7% and I need 35.70

Here is the whole code:
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim food, calories, grams As String
    Dim Fact_Calories, Answer, Answer1 As Double
Sub Input()
        food = txtfood.Text
        calories = CDbl(txtCalories.Text)
        grams = CDbl(txtGrams.Text)
    End Sub
    Sub output()
        lstOutput.Items.Clear()
        lstOutput.Items.Add(food & "Contains " & cal() & "% Calories from fat.")
        lstOutput.Items.Add(food & "if statement ")
    End Sub
    Function cal() As Double
        Fact_Calories = grams * 9
        Answer = Fact_Calories / calories * 100
        Format(Answer, "00.00%")
        Return Answer
    End Function

    Private Sub cmdCompute_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCompute.Click
        Input()
        output()
    End Sub
End Class
0
 
qing02051981Commented:
Should this give u percentage in two decimal places?
Format Percent (Answer, 2)
0
All Courses

From novice to tech pro — start learning today.