Solved

# VB.Net - Format % Calculation

Posted on 2012-08-24
430 Views
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
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") & "%"
0

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.

jimbo99999
0

LVL 74

Expert Comment

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

## Featured Post

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 (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". 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 (https://www.paessler.com/prtg) 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…