Solved

Visual Basic format functions

Posted on 2004-10-22
183 Views
Last Modified: 2010-04-17
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
0
Question by:jsctechy
    8 Comments
     
    LVL 18

    Expert Comment

    by:bobbit31
    Format(Answer*100, "#0.00%")
    0
     
    LVL 1

    Author Comment

    by:jsctechy
    it prints 0.00% and I want 00.00%, meaning 12.00%
    0
     
    LVL 18

    Expert Comment

    by:bobbit31
    Format(Answer*100, "00.00%")

    ;)
    0
     
    LVL 1

    Author Comment

    by:jsctechy
    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
     
    LVL 8

    Expert Comment

    by:bramsquad
    it shouldnt

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

    returns this

    35.70%

    0
     
    LVL 1

    Author Comment

    by:jsctechy
    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
     
    LVL 18

    Accepted Solution

    by:
    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
     
    LVL 1

    Expert Comment

    by:qing02051981
    Should this give u percentage in two decimal places?
    Format Percent (Answer, 2)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    This is about my first experience with programming Arduino.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    911 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now