feat: Added CalculatePercentage

This commit is contained in:
Mira 2023-04-24 14:49:29 +02:00
parent f53ce5d067
commit f68a186bc9
Signed by untrusted user who does not match committer: Xorog
GPG key ID: 983798ED9C3E7C36

View file

@ -618,6 +618,26 @@ public static class UniversalExtensions
return "#" + R.ToString("X2") + G.ToString("X2") + B.ToString("X2");
}
/// <summary>
/// Calculates the percentage of the given 2 values.
/// </summary>
/// <param name="current">The current value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The percentage.</returns>
/// <exception cref="ArgumentException"></exception>
public static int CalculatePercentage(double current, double max)
{
if (max == 0)
throw new ArgumentException("Max cannot be zero.");
double percentage = (current / max) * 100;
return Convert.ToInt32(percentage);
}
/// <summary>
/// Shorten a string to the given length
/// </summary>