
Percentages are often useful in programs. With the numbers one and two, we can get a percentage of 50%. We display and process percentages with doubles in the C# programming language—and solve an annoying rounding problem.

Here we see some code that uses string.Format to display your two numbers or ratio as a percentage. The following code shows four methods, with the first being Main and the last three being custom methods for displaying percentages.
These C# examples cover percentages. They show how to compute percentages and write to the console.
Program that calculates percents [C#]
using System;
class Program
{
static void Main()
{
// Display percentage of visits that resulted in purchases.
int purchases = 10;
int visitors = 120;
DisplayPercentage((double)purchases / visitors);
// Display 50 percent with overloaded method.
DisplayPercentage(1, 2);
// Write percentage string of nine tenths.
Console.WriteLine(GetPercentageString((double)9 / 10));
}
/// <summary>
/// This method writes the percentage form of a double to the console.
/// </summary>
static void DisplayPercentage(double ratio)
{
string percentage = string.Format("Percentage is {0:0.0%}", ratio);
Console.WriteLine(percentage);
}
/// <summary>
/// This method writes the percentage of the top number to the bottom number.
/// </summary>
static void DisplayPercentage(int top, int bottom)
{
DisplayPercentage((double)top / bottom);
}
/// <summary>
/// This method returns the percentage-formatted string.
/// </summary>
static string GetPercentageString(double ratio)
{
return ratio.ToString("0.0%");
}
}
Output
Percentage is 8.3%
Percentage is 50.0%
90.0%
Description. The Main method shown here calls the three bottom methods, which do the actual work for displaying or returning percentage-based strings. The output is shown in a comment at the end.
DisplayPercentage. This method accepts a double that is a ratio, usually between 0 and 1. It uses a custom formatting string to display the percentage to the Console. The {0:0.0%} substitution indicates you want a percentage with one decimal place.
Also: The second DisplayPercentage method here accepts two parameters and then passes the ratio of them to the first other method. It casts to double, which means it retains decimal places.
GetPercentageString. This method on the bottom accepts a double containing a ratio and returns a percentage string using ToString(). The ToString method and the string.Format method accept the same formatting characters.
Here we see how you can convert two integers into a percentage manually with division and multiplication. Sometimes you can need raw percentages when you have percentages in the database stored in different formats. The important part here is the cast to double, and the Math methods.
Program that converts ratios [C#]
using System;
class Program
{
static void Main()
{
// We want to have 92.9% from these two numbers.
int valid = 92;
int total = 99;
// First multiply top by 100 then divide.
double percent = (double)(valid * 100) / total; // <-- Use cast
// This is the percent number.
Console.WriteLine(percent);
Console.WriteLine(Math.Floor(percent));
Console.WriteLine(Math.Ceiling(percent));
Console.WriteLine(Math.Round(percent, 1));
}
}
Output
92.9292929292929
92
93
92.9
Casting to double. The double must be assigned to a value that is casted to (double). If you omit the (double) cast, your value will be rounded and probably useless. When casting to double, you do not need to surround the entire expression with parenthesis ( ). You can find more information on division and casting here.
Divide NumbersMethod calls. The final four statements in the program display different forms of the percentage. The Math.Floor method rounds down to the nearest integer; the Math.Ceiling rounds up to the nearest integer; and the Math.Round call here rounds to a single decimal place.
Math.Round Method
Here we mention that the percentage sign % in the C# programming language has a use as the modulo operator. This forms an expression that will return the remainder of a division of the two operands. You can find more about the modulo operator on this site.
Modulo Operator
We saw two examples of using percentages in the C# programming language. First we saw how to format ratios as percentages with three different methods. Second, we saw how to get a percentage value directly with math, and then round it. Importantly, we saw how to cast to double to maintain accuracy.
Number Examples