9

I have two values one with a decimal value and another value with a value which will calculate the percentage of that decimal value for example:

60 % of 10 = 6

decimal value1 = 10;
decimal percentage = 60;
textbox1.text = ("mathsum here").toString();

How would you calculate this value using the decimal value and value containing the percentage value?

FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
question21
  • 93
  • 1
  • 1
  • 3

10 Answers10

28
number * percentage / 100

so

10 * 60 / 100 = 6
BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • how would you work 100% of the value? sounds daft because it will be the value anyway but won't compile when inputting 100? – question21 Apr 07 '11 at 12:45
  • 1
    You should probably put `m` after those numbers just to be clear. `10 * 60 / 100` may work as integers, but `45 * 2 / 100` wouldn't. – Dan Tao Apr 07 '11 at 12:46
  • @question21: it's correct: 100% of 527 is 527 * 100 / 100 = 527 – BlackBear Apr 07 '11 at 12:46
  • 1
    @question21: You must have made a mistake. `10 * 100 / 100` will definitely come out to `10`. – Dan Tao Apr 07 '11 at 12:48
7

Maybe it will help you to think of it in this way.

6
-- = .6 (or equivalent to your 60%)
10

In your example you'd like to know how to calculate the numerator (the 6) so assign a variable to it. Let's use X.

X
-- = .6
10

.. and solve for X by multiplying both sides by 10 (in your case).

X * 10 = .6 * 10
------
  10

X = .6 * 10

From this I hope you can see that you can take your percentage value and multiply it by your 'decimal' value.

Note that in order to get the .6 you will need to convert your percentage (60) by dividing it by 100.

So our final formula is:

60
--- * 10
100 

or using your variables:

percentage
---------- * value1
   100

I hope I've added to your understanding even if my formula is similar to the previous answers. I wanted to make sure you understood how the formula was derived.

Good luck!

McArthey
  • 1,614
  • 30
  • 62
5
var result = (percentage/100) * value1;
textbox1.Text = result.ToString();
Richard Friend
  • 15,800
  • 1
  • 42
  • 60
4

You mean like this?

textbox1.text = (value1 * percentage/100).ToString();

By the way, toString is written ToString in C# with a capital T.

Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
3

To get the percentage amount

decimal Value = 1200;
int percentage = 20; //20%

var result=(percentage/100)*(Value);
Ramgy Borja
  • 2,330
  • 2
  • 19
  • 40
3
var answer = value1 * (percentage/100);
Barry
  • 2,053
  • 3
  • 17
  • 28
2

Wouldn't this just be

percentage/100m*value

?

Tovi7
  • 2,793
  • 3
  • 23
  • 27
1

I would separate the concerns:

  1. Calculate a portion of your original decimal:

    decimal result = (value * percentage) / 100.0;

  2. Provide an appropriate formatter to output the result as a percentage:

    text = result.ToString("0.0%");

http://www.dotnetperls.com/percentage

GregC
  • 7,737
  • 2
  • 53
  • 67
1

from question it self answer is clear

60% means 60/100 then calculate it with the value

60 / 100 * 10 = 6 use the logic for variables

 textbox1.Text = ((percentage /100) * value).ToString();

or

 textbox1.Text = ((percentage * .01 ) * value).ToString();
Nighil
  • 4,099
  • 7
  • 30
  • 56
0

You need to divide by 100.

60% = 60/100.

Giovanni Galbo
  • 12,963
  • 13
  • 59
  • 78