VB.Net - Format % Calculation

Posted on 2012-08-24
Last Modified: 2012-08-24
Good Day Experts!

I am kind of "stuck" on this seemingly simple idea.  I calculate a percentage and format it to 2 decimal places:

     PercentageLessThan30 = ((lessThan30AmountOfBill / TotalAmountOfBill) * 100)
     PercentageLessThan30 = Format(PercentageLessThan30, "###0.00")

PercentageLessThan30 is defined as Decimal.  So, how do I add the percent sign to the end of the numerical value so I can output it to Excel?

Question by:Jimbo99999
    LVL 85

    Accepted Solution

    "PercentageLessThan30 is defined as Decimal"

    Use a separate variable defined as a STRING, and append your percent sign to it...

        Dim strPercentage As String = PercentageLessThan30.ToString("###0.00") & "%"

    Author Comment

    Excellent...I will implement when the current iteration of the program completes running.  It takes a long time since there are 18million records in the table.

    LVL 74

    Expert Comment

    by:käµfm³d 👽
    You can do it in one operation if you use the appropriate format modifers:

    Module Module1
        Sub Main()
            Dim lessThan30AmountOfBill As Decimal = 25
            Dim TotalAmountOfBill As Decimal = 100
            Dim PercentageLessThan30 As Decimal
            Dim value As String
            PercentageLessThan30 = ((lessThan30AmountOfBill / TotalAmountOfBill))
            value = String.Format("{0:###0.00%}", PercentageLessThan30)
        End Sub
    End Module

    Open in new window

    Note the usage of String.Format, the inclusion of the "%" to the format, and the removal of the "* 100" from the math. See this nice listing of format operators for a description of what the "%" is doing.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    761 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

    9 Experts available now in Live!

    Get 1:1 Help Now