• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 451
  • 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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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