• Status: Solved
• Priority: Medium
• Security: Public
• Views: 451

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?

Thanks,
jimbo99999
0
Jimbo99999
1 Solution

Middle 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") & "%"
0

Author 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.

jimbo99999
0

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)

Console.WriteLine(value)
End Sub

End Module
``````

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.
0
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.