diff --git a/CsuChhs.Extensions.Tests/NumberExtensionsTest.cs b/CsuChhs.Extensions.Tests/NumberExtensionsTest.cs index 0d5aba9..a59a595 100644 --- a/CsuChhs.Extensions.Tests/NumberExtensionsTest.cs +++ b/CsuChhs.Extensions.Tests/NumberExtensionsTest.cs @@ -151,5 +151,27 @@ public void TestToPercentAsString() Assert.Equal("65.217", number.ToPercent(total, 3).ToString()); Assert.Equal("65", number.ToIntPercent(total).ToString()); } + + [Fact] + public void TestToPercentOrZero() + { + double number, total; + { + number = 31; + total = 57; + Assert.Equal(54.0, number.ToPercentOrZero(total)); + Assert.Equal(54, number.ToIntPercentOrZero(total)); + Assert.Equal(54.4, number.ToPercentOrZero(total, 1)); + Assert.Equal(54.39, number.ToPercentOrZero(total, 2)); + Assert.Equal(54.386, number.ToPercentOrZero(total, 3)); + } + + { + number = 500; + total = 0; + Assert.Equal(0.0, number.ToPercentOrZero(total)); + Assert.Equal(0, number.ToIntPercentOrZero(total)); + } + } } } diff --git a/CsuChhs.Extensions/CsuChhs.Extensions.csproj b/CsuChhs.Extensions/CsuChhs.Extensions.csproj index 747c99a..1f895d1 100644 --- a/CsuChhs.Extensions/CsuChhs.Extensions.csproj +++ b/CsuChhs.Extensions/CsuChhs.Extensions.csproj @@ -6,7 +6,7 @@ enable true CHHS Application Development Team - 1.4.0 + 1.4.1 https://github.com/csu-chhs/CsuChhs.Extensions College of Health and Human Sciences CsuChhs.Extensions diff --git a/CsuChhs.Extensions/NumberExtensions.cs b/CsuChhs.Extensions/NumberExtensions.cs index 34ab799..56c7746 100644 --- a/CsuChhs.Extensions/NumberExtensions.cs +++ b/CsuChhs.Extensions/NumberExtensions.cs @@ -54,7 +54,8 @@ public static double TruncateDecimals(this double num, int roundTo) } /// - /// Provides the percent that one number represents of another. + /// Provides the percent that one number represents of a total. + /// If the total is 0, throws a . /// /// The number you have /// The total number from which you want to derive your number's percentage @@ -73,6 +74,7 @@ public static double ToPercent(this double num, double total, int decimalPlaces /// /// Quick way to get a simple integer number representing your number's percentage of another number. /// Rounds up or down to nearest whole number. + /// If the total is 0, throws a . /// /// The number you have /// The total number from which you want to derive your number's percentage @@ -88,5 +90,39 @@ public static int ToIntPercent(this double num, double total) throw ex; } } + + /// + /// Provides the percent that one number represents of a total. + /// If the total is 0, it just returns 0. Does not throw an exception. + /// + /// + /// + /// + /// + public static double ToPercentOrZero(this double num, double total, int decimalPlaces = 0) + { + if (total == 0.0) + { + return 0.0; + } + return num.ToPercent(total, decimalPlaces); + } + + /// + /// Quick way to get a simple integer number representing your number's percentage of another number. + /// Rounds up or down to nearest whole number. + /// If the total is 0, it just returns 0. Does not throw an exception. + /// + /// + /// + /// + public static int ToIntPercentOrZero(this double num, double total) + { + if (total == 0.0) + { + return 0; + } + return num.ToIntPercent(total); + } } }