How to clalculate percent difference between two vlaues

Posted on 2009-02-18
Last Modified: 2012-06-22
Say I have two labels on my page:

Label 1 has a value of 100
Label 2 has a value of 140

How do I calculate the % difference between these 2 values in VB?
Question by:JessyRobinson1234
    LVL 39

    Expert Comment

    That should be:

    dim percentage As Integer = Abs(val1 / val2 * 100 - 100)

    LVL 39

    Accepted Solution

    To go from a label to a number, use:


    So, the whole code may become:

    Dim percentage As Integer = Abs(Integer.Parse(Label1.Text) / Integer.Parse(Label2.Text) * 100 - 100)

    Of course, the order matters. From 100 to 140 is a 40% increase, from 140 to 100 is 28.5% decrease. You may remove ABS if you want to know whether it is increase or decrease:

    From 140 to 100:

    100 / 140 = 0.7143
    0.7143 * 100 = 71.43
    71.43 - 100 = -28.57

    From 100 to 140

    140 / 100 = 1.4
    1.4 * 100 = 140
    140 - 100 = +40
    LVL 10

    Expert Comment


    Will give you the % difference between them in terms of label1%
    LVL 39

    Expert Comment

    That'd be the same road to heaven, differently formulated ;)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now