Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Is there a way to format a percentage of a value without doing any math?

In VS 2005, is there a way to format a value by percentage, like: Value = 50% of Value without doing any math (5 = 50% of 10)?

2 Solutions
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure exactly what you are asking here...

Like this?

        Dim value As Integer = 10
        Dim str As String = (0.5 * value).ToString("0")

You have to do the "math" by multiplying by .5 to get 50%.  There isn't anything you could place in the ToString() portion that would do this for you if that is what you are asking...
Dim str As String =  (5.0 / 10.0).ToString("00%")
you will get your 50%
It will not work without math, unless you write a small function

    Private Function Percent(ByVal v As Double, ByVal v100 As Double) As String
        Return (v / v100).ToString("00%")
    End Function

Dim str As String =Percent(5.0,10.0) ' this is as math-free as it gets


grouchyfdAuthor Commented:
Thank you both. This is what I was looking for. One time I can just incorporate the code, the other I can make a public/private function  so an app. can do the same thing several times.

Featured Post

Industry Leaders: 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