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

Add support for long to ToQuantity #504

Closed
Closed
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class SomeClass

string FormatSomeClass(SomeClass sc)
{
return string.Format("SomeObject #{0} - {1}", sc.SomeInt, sc.SomeString);
return string.Format("SomeObject #{0} - {1}", sc.SomeInt, sc.SomeString);
}

var collection = new List<SomeClass>
Expand Down
25 changes: 25 additions & 0 deletions src/Humanizer.Tests.Shared/NumberToWordsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ public void ToWords(int number, string expected)
Assert.Equal(expected, number.ToWords());
}

[InlineData(1L, "one")]
[InlineData(11L, "eleven")]
[InlineData(111L, "one hundred and eleven")]
[InlineData(1111L, "one thousand one hundred and eleven")]
[InlineData(11111L, "eleven thousand one hundred and eleven")]
[InlineData(111111L, "one hundred and eleven thousand one hundred and eleven")]
[InlineData(1111111L, "one million one hundred and eleven thousand one hundred and eleven")]
[InlineData(11111111L, "eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(111111111L, "one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(1111111111L, "one billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(11111111111L, "eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(111111111111L, "one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(1111111111111L, "one trillion one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(11111111111111L, "eleven trillion one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(111111111111111L, "one hundred and eleven trillion one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(1111111111111111L, "one quadrillion one hundred and eleven trillion one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(11111111111111111L, "eleven quadrillion one hundred and eleven trillion one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(111111111111111111L, "one hundred and eleven quadrillion one hundred and eleven trillion one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[InlineData(1111111111111111111L, "one quintillion one hundred and eleven quadrillion one hundred and eleven trillion one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven")]
[Theory]
public void ToWords(long number, string expected)
{
Assert.Equal(expected, number.ToWords());
}

[Theory]
[InlineData(0, "zeroth")]
[InlineData(1, "first")]
Expand Down
6 changes: 6 additions & 0 deletions src/Humanizer.Tests.Shared/ToQuantityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ToQuantityTests
public void ToQuantity(string word, int quantity, string expected)
{
Assert.Equal(expected, word.ToQuantity(quantity));
Assert.Equal(expected, word.ToQuantity((long)quantity));
}

[Theory]
Expand All @@ -40,6 +41,7 @@ public void ToQuantity(string word, int quantity, string expected)
public void ToQuantityWithNoQuantity(string word, int quantity, string expected)
{
Assert.Equal(expected, word.ToQuantity(quantity, ShowQuantityAs.None));
Assert.Equal(expected, word.ToQuantity((long)quantity, ShowQuantityAs.None));
}

[Theory]
Expand All @@ -58,6 +60,7 @@ public void ToQuantityNumeric(string word, int quantity, string expected)
{
// ReSharper disable once RedundantArgumentDefaultValue
Assert.Equal(expected, word.ToQuantity(quantity, ShowQuantityAs.Numeric));
Assert.Equal(expected, word.ToQuantity((long)quantity, ShowQuantityAs.Numeric));
}

[Theory]
Expand All @@ -76,6 +79,7 @@ public void ToQuantityNumeric(string word, int quantity, string expected)
public void ToQuantityWords(string word, int quantity, string expected)
{
Assert.Equal(expected, word.ToQuantity(quantity, ShowQuantityAs.Words));
Assert.Equal(expected, word.ToQuantity((long)quantity, ShowQuantityAs.Words));
}

[Theory]
Expand All @@ -93,6 +97,7 @@ public void ToQuantityWords(string word, int quantity, string expected)
public void ToQuantityWordsWithCurrentCultureFormatting(string word, int quantity, string format, string expected)
{
Assert.Equal(expected, word.ToQuantity(quantity, format));
Assert.Equal(expected, word.ToQuantity((long)quantity, format));
}

[Theory]
Expand All @@ -110,6 +115,7 @@ public void ToQuantityWordsWithCustomCultureFormatting(string word, int quantity
var culture = new CultureInfo(cultureCode);

Assert.Equal(expected, word.ToQuantity(quantity, format, culture), GetStringComparer(culture));
Assert.Equal(expected, word.ToQuantity((long)quantity, format, culture), GetStringComparer(culture));
}

internal static StringComparer GetStringComparer(CultureInfo culture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ namespace Humanizer
public static string ToOrdinalWords(this int number, Humanizer.GrammaticalGender gender, System.Globalization.CultureInfo culture = null) { }
public static string ToWords(this int number, System.Globalization.CultureInfo culture = null) { }
public static string ToWords(this int number, Humanizer.GrammaticalGender gender, System.Globalization.CultureInfo culture = null) { }
public static string ToWords(this long number, System.Globalization.CultureInfo culture = null) { }
public static string ToWords(this long number, Humanizer.GrammaticalGender gender, System.Globalization.CultureInfo culture = null) { }
}
public class On
{
Expand Down Expand Up @@ -961,6 +963,8 @@ namespace Humanizer
{
public static string ToQuantity(this string input, int quantity, Humanizer.ShowQuantityAs showQuantityAs = 1) { }
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 class static TruncateExtensions
{
Expand Down Expand Up @@ -1099,8 +1103,8 @@ namespace Humanizer.Localisation.NumberToWords

public interface INumberToWordsConverter
{
string Convert(int number);
string Convert(int number, Humanizer.GrammaticalGender gender);
string Convert(long number);
string Convert(long number, Humanizer.GrammaticalGender gender);
string ConvertToOrdinal(int number);
string ConvertToOrdinal(int number, Humanizer.GrammaticalGender gender);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Humanizer.v2.ncrunchsolution
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<SolutionConfiguration>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this file needed? Isn't it only a configuration for NCrunch?

Copy link
Author

Choose a reason for hiding this comment

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

good catch, I have removed it

<FileVersion>1</FileVersion>
<InferProjectReferencesUsingAssemblyNames>false</InferProjectReferencesUsingAssemblyNames>
<AllowParallelTestExecution>false</AllowParallelTestExecution>
<AllowTestsToRunInParallelWithThemselves>true</AllowTestsToRunInParallelWithThemselves>
<FrameworkUtilisationTypeForNUnit>UseDynamicAnalysis</FrameworkUtilisationTypeForNUnit>
<FrameworkUtilisationTypeForGallio>UseStaticAnalysis</FrameworkUtilisationTypeForGallio>
<FrameworkUtilisationTypeForMSpec>UseStaticAnalysis</FrameworkUtilisationTypeForMSpec>
<FrameworkUtilisationTypeForMSTest>UseStaticAnalysis</FrameworkUtilisationTypeForMSTest>
<FrameworkUtilisationTypeForXUnitV2>UseStaticAnalysis</FrameworkUtilisationTypeForXUnitV2>
<MetricsExclusionList>
</MetricsExclusionList>
</SolutionConfiguration>
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ internal class AfrikaansNumberToWordsConverter : GenderlessNumberToWordsConverte
{19, "negentiende"}
};

public override string Convert(int number)
public override string Convert(long number)
{
return Convert(number, false);
if(number > Int32.MaxValue|| number < Int32.MinValue)
{
throw new NotImplementedException();
}
return Convert((int)number, false);
}

public override string ConvertToOrdinal(int number)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ internal class ArabicNumberToWordsConverter : GenderlessNumberToWordsConverter
private static readonly string[] AppendedTwos = { "مئتان", "ألفان", "مليونان", "ملياران", "تريليونان", "كوادريليونان", "كوينتليونان", "سكستيليونلن" };
private static readonly string[] Twos = { "مئتان", "ألفان", "مليونان", "ملياران", "تريليونان", "كوادريليونان", "كوينتليونان", "سكستيليونان" };

public override string Convert(int number)
public override string Convert(long input)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
throw new NotImplementedException();
}
var number = (int)input;

if (number == 0)
return "صفر";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Humanizer.Localisation.NumberToWords
{
Expand Down Expand Up @@ -59,8 +60,14 @@ public override string ConvertToOrdinal(int number)
return Convert(number) + " তম";
}

public override string Convert(int number)
public override string Convert(long input)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
throw new NotImplementedException();
}
var number = (int)input;

if (number == 0)
return UnitsMap[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ internal class BrazilianPortugueseNumberToWordsConverter : GenderedNumberToWords
private static readonly string[] PortugueseOrdinalTensMap = { "zero", "décimo", "vigésimo", "trigésimo", "quadragésimo", "quinquagésimo", "sexagésimo", "septuagésimo", "octogésimo", "nonagésimo" };
private static readonly string[] PortugueseOrdinalHundredsMap = { "zero", "centésimo", "ducentésimo", "trecentésimo", "quadringentésimo", "quingentésimo", "sexcentésimo", "septingentésimo", "octingentésimo", "noningentésimo" };

public override string Convert(int number, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
throw new NotImplementedException();
}
var number = (int)input;

if (number == 0)
return "zero";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public DefaultNumberToWordsConverter(CultureInfo culture)
/// </summary>
/// <param name="number">Number to be turned to words</param>
/// <returns></returns>
public override string Convert(int number)
public override string Convert(long number)
{
return number.ToString(_culture);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Humanizer.Localisation.NumberToWords
Expand Down Expand Up @@ -31,8 +32,14 @@ class Fact
new Fact {Value = 100, Name = "honderd", Prefix = "", Postfix = "", DisplayOneUnit = false}
};

public override string Convert(int number)
public override string Convert(long input)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
throw new NotImplementedException();
}
var number = (int)input;

if (number == 0)
return UnitsMap[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class EnglishNumberToWordsConverter : GenderlessNumberToWordsConverter
private static readonly string[] UnitsMap = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
private static readonly string[] TensMap = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

private static readonly Dictionary<int, string> OrdinalExceptions = new Dictionary<int, string>
private static readonly Dictionary<long, string> OrdinalExceptions = new Dictionary<long, string>
{
{1, "first"},
{2, "second"},
Expand All @@ -20,7 +20,7 @@ internal class EnglishNumberToWordsConverter : GenderlessNumberToWordsConverter
{12, "twelfth"},
};

public override string Convert(int number)
public override string Convert(long number)
{
return Convert(number, false);
}
Expand All @@ -30,7 +30,7 @@ public override string ConvertToOrdinal(int number)
return Convert(number, true);
}

private string Convert(int number, bool isOrdinal)
private string Convert(long number, bool isOrdinal)
{
if (number == 0)
return GetUnitValue(0, isOrdinal);
Expand All @@ -40,6 +40,24 @@ private string Convert(int number, bool isOrdinal)

var parts = new List<string>();

if ((number / 1000000000000000000) > 0)
{
parts.Add(string.Format("{0} quintillion", Convert(number / 1000000000000000000)));
number %= 1000000000000000000;
}

if ((number / 1000000000000000) > 0)
{
parts.Add(string.Format("{0} quadrillion", Convert(number / 1000000000000000)));
number %= 1000000000000000;
}

if ((number / 1000000000000) > 0)
{
parts.Add(string.Format("{0} trillion", Convert(number / 1000000000000)));
number %= 1000000000000;
}

if ((number / 1000000000) > 0)
{
parts.Add(string.Format("{0} billion", Convert(number / 1000000000)));
Expand Down Expand Up @@ -93,7 +111,7 @@ private string Convert(int number, bool isOrdinal)
return toWords;
}

private static string GetUnitValue(int number, bool isOrdinal)
private static string GetUnitValue(long number, bool isOrdinal)
{
if (isOrdinal)
{
Expand All @@ -116,7 +134,7 @@ private static string RemoveOnePrefix(string toWords)
return toWords;
}

private static bool ExceptionNumbersToWords(int number, out string words)
private static bool ExceptionNumbersToWords(long number, out string words)
{
return OrdinalExceptions.TryGetValue(number, out words);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ internal class FarsiNumberToWordsConverter : GenderlessNumberToWordsConverter
private static readonly string[] FarsiTensMap = { "صفر", "ده", "بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود" };
private static readonly string[] FarsiUnitsMap = { "صفر", "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه", "ده", "یازده", "دوازده", "سیزده", "چهارده", "پانزده", "شانزده", "هفده", "هجده", "نوزده" };

public override string Convert(int number)
public override string Convert(long input)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
throw new NotImplementedException();
}
var number = (int)input;

if (number < 0)
return string.Format("منفی {0}", Convert(-number));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Humanizer.Localisation.NumberToWords
{
Expand All @@ -13,8 +14,14 @@ internal class FinnishNumberToWordsConverter : GenderlessNumberToWordsConverter
{2, "kahdes" }
};

public override string Convert(int number)
public override string Convert(long input)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
throw new NotImplementedException();
}
var number = (int)input;

if (number < 0)
return string.Format("miinus {0}", Convert(-number));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ internal class FrenchNumberToWordsConverter : GenderedNumberToWordsConverter
{91, "quatre-vingt-onze"}
};

public override string Convert(int number, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
throw new NotImplementedException();
}
var number = (int)input;

if (number == 0)
return UnitsMap[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected GenderedNumberToWordsConverter(GrammaticalGender defaultGender = Gramm
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public string Convert(int number)
public string Convert(long number)
{
return Convert(number, _defaultGender);
}
Expand All @@ -25,7 +25,7 @@ public string Convert(int number)
/// <param name="number"></param>
/// <param name="gender"></param>
/// <returns></returns>
public abstract string Convert(int number, GrammaticalGender gender);
public abstract string Convert(long number, GrammaticalGender gender);

/// <summary>
/// Converts the number to ordinal string using the locale's default grammatical gender
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ abstract class GenderlessNumberToWordsConverter : INumberToWordsConverter
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public abstract string Convert(int number);
public abstract string Convert(long number);

/// <summary>
/// Converts the number to string ignoring the provided grammatical gender
/// </summary>
/// <param name="number"></param>
/// <param name="gender"></param>
/// <returns></returns>
public string Convert(int number, GrammaticalGender gender)
public string Convert(long number, GrammaticalGender gender)
{
return Convert(number);
}
Expand Down
Loading