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

Posted on 2006-04-25
Last Modified: 2010-04-23
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)?

Question by:grouchyfd
    LVL 85

    Accepted Solution

    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...
    LVL 9

    Assisted Solution

    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


    Author Comment

    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

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now