HOWTO · Csharp
How to Do Percentage Calculation in C#
Learn how to calculate a percentage of a value, increase a value, and decrease a value in C#.
On this page
A percentage expresses a part as a number out of 100. In C#, divide the percentage by 100 before multiplying by the value. The same relationship gives you the amount to add for an increase or subtract for a decrease.
Understanding Percentage Calculations
A percentage is a fraction of 100: 15% means 15 out of every 100. To find a percentage of a total, first convert the percentage to a decimal by dividing it by 100, then multiply by the total. For example, 15% of 200 is (15 / 100) * 200, which equals 30.
Keep the percentage amount separate from the adjusted total. For an increase, calculate the increase amount and add it to the original value. For a decrease, calculate the decrease amount and subtract it. This distinction helps avoid returning only the change when the task asks for the new total.
Basic Percentage Calculation
The formula for a percentage of a value is:
amount = (percentage / 100) * total
This complete C# program calculates 15% of 200. It uses double so dividing by 100 keeps the fractional part of the calculation.
Run this example:
using System;
class Program
{
static void Main()
{
double total = 200;
double percentage = 15;
double amount = (percentage / 100) * total;
Console.WriteLine($"15% of 200 is {amount}");
}
}
If you are calculating a score as a percentage, divide the marks earned by the total possible marks, then multiply by 100. For instance, if a test is marked out of 80, the denominator is 80; dividing marks by four would calculate an average only if the four values were separate marks, not a percentage of a known maximum.
Percentage Increase Calculation
To increase an original value by a percentage, calculate the increase amount and add it to the original:
new value = original value + (increase percentage / 100 * original value)
For an original value of 100 and a 20% increase, the increase amount is 20 and the resulting value is 120. This program shows both values so you can adapt the calculation to a price, quantity, or other numeric amount.
using System;
class Program
{
static void Main()
{
double originalValue = 100;
double increasePercentage = 20;
double increaseAmount = (increasePercentage / 100) * originalValue;
double newValue = originalValue + increaseAmount;
Console.WriteLine($"Increase amount: {increaseAmount}");
Console.WriteLine($"New value: {newValue}");
}
}
Percentage Decrease Calculation
To decrease a value, calculate the decrease amount from the original value, then subtract it:
new value = original value - (decrease percentage / 100 * original value)
For example, a 10% discount on a price of 150 is 15, leaving a final price of 135. A discount percentage applies to the original price; subtracting the percentage number itself would be incorrect because 10 is not necessarily 10% of the price.
using System;
class Program
{
static void Main()
{
double originalPrice = 150;
double discountPercentage = 10;
double discountAmount = (discountPercentage / 100) * originalPrice;
double finalPrice = originalPrice - discountAmount;
Console.WriteLine($"Discount amount: {discountAmount}");
Console.WriteLine($"Final price: {finalPrice}");
}
}
For financial applications, use decimal rather than double when decimal precision and currency rounding matter. Choose and apply a rounding rule at the point your application needs a currency amount; the percentage formulas themselves do not determine that business rule.
Common Questions
How do I calculate a percentage of a number in C#?
Divide the percentage by 100, then multiply by the number. For example, 15% of 200 is (15 / 100) * 200, or 30.
How do I calculate the percentage increase?
Multiply the original value by the increase percentage divided by 100 to get the increase amount, then add that amount to the original value.
How do I calculate a percentage decrease or discount?
Multiply the original value by the decrease percentage divided by 100, then subtract the decrease amount from the original value.
How do I calculate marks as a percentage?
Use (marks earned / total possible marks) * 100. The denominator must be the maximum possible score for the same test or set of marks; an average of several scores answers a different question.