• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 438
  • Last Modified:

VB.Net - Format % Calculation

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?

1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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") & "%"
Jimbo99999Author Commented:
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.

käµfm³d 👽Commented:
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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now