Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added string formatting options to ToQuantity #220

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ You can also pass a second argument, `ShowQuantityAs`, to `ToQuantity` to specif
"case".ToQuantity(5, ShowQuantityAs.None) => "cases"
```

There is also an overload that allows you to format the number. You can pass in the format and the culture to be used.

```C#
"dollar".ToQuantity(2, "C0", new CultureInfo("en-US")) => "$2 dollars"
"dollar".ToQuantity(2, "C2", new CultureInfo("en-US")) => "$2.00 dollars"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty cool. Thanks for the great addition.

"cases".ToQuantity(12000, "N0") => "12,000 cases"
```

####<a id="ordinalize">Ordinalize</a>
`Ordinalize` turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public class To
public class ToQuantityExtensions
{
public string ToQuantity(string input, int quantity, Humanizer.ShowQuantityAs showQuantityAs) { }
public string ToQuantity(string input, int quantity, string format, System.IFormatProvider formatProvider) { }
}

public class Truncator
Expand Down
37 changes: 36 additions & 1 deletion src/Humanizer.Tests/ToQuantityTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Xunit;
using System;
using System.Globalization;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests
Expand Down Expand Up @@ -79,5 +81,38 @@ public void ToQuantityWords(string word, int quatity, string expected)
{
Assert.Equal(expected, word.ToQuantity(quatity, ShowQuantityAs.Words));
}

[Theory]
[InlineData("case", 0, null, "0 cases")]
[InlineData("case", 1, null, "1 case")]
[InlineData("case", 2, null, "2 cases")]
[InlineData("case", 1, "N0", "1 case")]
[InlineData("case", 2, "N0", "2 cases")]
[InlineData("case", 123456, "N0", "123,456 cases")]
[InlineData("case", 123456, "N2", "123,456.00 cases")]
[InlineData("dollar", 0, "C0", "$0 dollars")]
[InlineData("dollar", 1, "C0", "$1 dollar")]
[InlineData("dollar", 2, "C0", "$2 dollars")]
[InlineData("dollar", 2, "C2", "$2.00 dollars")]
public void ToQuantityWordsWithCurrentCultureFormatting(string word, int quantity, string format, string expected)
{
Assert.Equal(expected, word.ToQuantity(quantity, format));
}

[Theory]
[InlineData("case", 0, "N0", "it-IT", "0 cases")]
[InlineData("case", 1, "N0", "it-IT", "1 case")]
[InlineData("case", 2, "N0", "it-IT", "2 cases")]
[InlineData("case", 1234567, "N0", "it-IT", "1.234.567 cases")]
[InlineData("case", 1234567, "N2", "it-IT", "1.234.567,00 cases")]
[InlineData("euro", 0, "C0", "es-ES", "0 € euros")]
[InlineData("euro", 1, "C0", "es-ES", "1 € euro")]
[InlineData("euro", 2, "C0", "es-ES", "2 € euros")]
[InlineData("euro", 2, "C2", "es-ES", "2,00 € euros")]
public void ToQuantityWordsWithCustomCultureFormatting(string word, int quantity, string format, string cultureCode, string expected)
{
var culture = new CultureInfo(cultureCode);
Assert.Equal(expected, word.ToQuantity(quantity, format, culture), StringComparer.Create(culture, false));
}
}
}
28 changes: 26 additions & 2 deletions src/Humanizer/ToQuantityExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Humanizer
using System;
namespace Humanizer
{
/// <summary>
/// Enumerates the ways of displaying a quantity value when converting
Expand Down Expand Up @@ -44,6 +45,29 @@ public static class ToQuantityExtensions
/// </example>
/// <returns></returns>
public static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric)
{
return input.ToQuantity(quantity, showQuantityAs, format: null, formatProvider: null);
}

/// <summary>
/// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word
/// </summary>
/// <param name="input">The word to be prefixes</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) => "0 requests"
/// "request".ToQuantity(10000, format: "N0") => "10,000 requests"
/// "request".ToQuantity(1, format: "N0") => "1 request"
/// </example>
/// <returns></returns>
public static string ToQuantity(this string input, int quantity, string format, IFormatProvider formatProvider = null)
{
return input.ToQuantity(quantity, showQuantityAs: ShowQuantityAs.Numeric, format: format, formatProvider: formatProvider);
}

private static string ToQuantity(this string input, int quantity, ShowQuantityAs showQuantityAs = ShowQuantityAs.Numeric, string format = null, IFormatProvider formatProvider = null)
{
var transformedInput = quantity == 1
? input.Singularize(Plurality.CouldBeEither)
Expand All @@ -53,7 +77,7 @@ public static string ToQuantity(this string input, int quantity, ShowQuantityAs
return transformedInput;

if (showQuantityAs == ShowQuantityAs.Numeric)
return string.Format("{0} {1}", quantity, transformedInput);
return string.Format(formatProvider, "{0} {1}", quantity.ToString(format, formatProvider), transformedInput);

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