Skip to content

Commit

Permalink
Merge pull request #801 from AKTheKnight/fix-722
Browse files Browse the repository at this point in the history
Fix #722 Add ToQuantity for double and tests
  • Loading branch information
Oren Novotny authored Mar 31, 2019
2 parents 81c6dbe + 0871b95 commit 6b0deb3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Humanizer.Tests.Shared/ToQuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
28 changes: 28 additions & 0 deletions src/Humanizer/ToQuantityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,33 @@ private static string ToQuantity(this string input, long quantity, ShowQuantityA

return string.Format("{0} {1}", quantity.ToWords(), transformedInput);
}

/// <summary>
/// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word
/// </summary>
/// <param name="input">The word to be prefixed</param>
/// <param name="quantity">The quantity of the word</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <example>
/// "request".ToQuantity(0.2) => "0.2 requests"
/// "request".ToQuantity(10.6, format: "N0") => "10.6 requests"
/// "request".ToQuantity(1.0, format: "N0") => "1 request"
/// </example>
/// <returns></returns>
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);
}
}
}

0 comments on commit 6b0deb3

Please sign in to comment.