[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

How to clalculate percent difference between two vlaues

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?
0
JessyRobinson1234
Asked:
JessyRobinson1234
  • 3
1 Solution
 
abelCommented:
That should be:

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


0
 
abelCommented:
To go from a label to a number, use:

Integer.Parse(Label1.Text)

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
0
 
aboo_sCommented:
(100/label1)*(label2-label1)

Will give you the % difference between them in terms of label1%
0
 
abelCommented:
That'd be the same road to heaven, differently formulated ;)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now