diff --git a/src/Humanizer.Tests.Shared/ToQuantityTests.cs b/src/Humanizer.Tests.Shared/ToQuantityTests.cs index bf942d1ba..ad46f61fb 100644 --- a/src/Humanizer.Tests.Shared/ToQuantityTests.cs +++ b/src/Humanizer.Tests.Shared/ToQuantityTests.cs @@ -62,6 +62,16 @@ public void ToQuantityNumeric(string word, int quantity, string expected) Assert.Equal(expected, word.ToQuantity((long)quantity, ShowQuantityAs.Numeric)); } + [Theory] + [InlineData("hour", 1, "1 hour")] + [InlineData("hour", 0.5, "0.5 hours")] + [InlineData("hour", 22.4, "22.4 hours")] + public void ToDoubleQuantityNumeric(string word, double quantity, string expected) + { + // ReSharper disable once RedundantArgumentDefaultValue + Assert.Equal(expected, word.ToQuantity(quantity)); + } + [Theory] [InlineData("case", 0, "zero cases")] [InlineData("case", 1, "one case")] diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index e073cc484..fad778dad 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -1039,6 +1039,8 @@ namespace Humanizer public static string ToQuantity(this string input, int quantity, string format, System.IFormatProvider formatProvider = null) { } public static string ToQuantity(this string input, long quantity, Humanizer.ShowQuantityAs showQuantityAs = 1) { } public static string ToQuantity(this string input, long quantity, string format, System.IFormatProvider formatProvider = null) { } + public static string ToQuantity(this string input, double quantity, string format = null, System.IFormatProvider formatProvider = null) { } + public static string ToQuantity(this string input, double quantity) { } } public class static TruncateExtensions { diff --git a/src/Humanizer/ToQuantityExtensions.cs b/src/Humanizer/ToQuantityExtensions.cs index 90ad2cc77..ac40dbfba 100644 --- a/src/Humanizer/ToQuantityExtensions.cs +++ b/src/Humanizer/ToQuantityExtensions.cs @@ -122,5 +122,33 @@ private static string ToQuantity(this string input, long quantity, ShowQuantityA return string.Format("{0} {1}", quantity.ToWords(), transformedInput); } + + /// + /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word + /// + /// The word to be prefixed + /// The quantity of the word + /// A standard or custom numeric format string. + /// An object that supplies culture-specific formatting information. + /// + /// "request".ToQuantity(0.2) => "0.2 requests" + /// "request".ToQuantity(10.6, format: "N0") => "10.6 requests" + /// "request".ToQuantity(1.0, format: "N0") => "1 request" + /// + /// + public static string ToQuantity(this string input, double quantity, string format = null, IFormatProvider formatProvider = null) + { + var transformedInput = quantity == 1 + ? input.Singularize(inputIsKnownToBePlural: false) + : input.Pluralize(inputIsKnownToBeSingular: false); + + return string.Format(formatProvider, "{0} {1}", quantity.ToString(format, formatProvider), transformedInput); + + } + + public static string ToQuantity(this string input, double quantity) + { + return ToQuantity(input, quantity, null, null); + } } }