Skip to content

Commit

Permalink
Merge pull request #985 from AlexCinetic/number-and-overload
Browse files Browse the repository at this point in the history
Number and overload
  • Loading branch information
clairernovotny authored Oct 1, 2020
2 parents 80b5789 + 38deda7 commit c5d77ba
Show file tree
Hide file tree
Showing 23 changed files with 77 additions and 25 deletions.
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,14 @@ Also, culture to use can be specified explicitly. If it is not, current thread's
1.ToWords(GrammaticalGender.Masculine, new CultureInfo("ru")) => "один"
```

Another overload of the method allow you to pass a bool to remove the "And" that can be added before the last number:

```C#
3501.ToWords(false) => "three thousand five hundred one"
102.ToWords(false) => "one hundred two"
```
This method can be useful for writing checks for example.

### <a id="number-toordinalwords">Number to ordinal words</a>
This is kind of mixing `ToWords` with `Ordinalize`. You can call `ToOrdinalWords` on a number to get an ordinal representation of the number in words! For example:

Expand Down
7 changes: 7 additions & 0 deletions src/Humanizer.Tests.Shared/NumberToWordsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public void ToWordsInt(int number, string expected)
Assert.Equal(expected, number.ToWords());
}

[InlineData(3501L, "three thousand five hundred one", false)]
[Theory]
public void ToWordsWithoutAnd(int number, string expected, bool addAnd)
{
Assert.Equal(expected, number.ToWords(addAnd));
}

[InlineData(1L, "one")]
[InlineData(11L, "eleven")]
[InlineData(111L, "one hundred and eleven")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class ArabicNumberToWordsConverter : GenderedNumberToWordsConverter

private static readonly string[] FeminineOnesGroup = { "", "واحدة", "اثنتان", "ثلاث", "أربع", "خمس", "ست", "سبع", "ثمان", "تسع", "عشر", "إحدى عشرة", "اثنتا عشرة", "ثلاث عشرة", "أربع عشرة", "خمس عشرة", "ست عشرة", "سبع عشرة", "ثمان عشرة", "تسع عشرة" };

public override string Convert(long number, GrammaticalGender gender)
public override string Convert(long number, GrammaticalGender gender, bool addAnd = true)
{
if (number == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ 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(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
if (input > 999999999999 || input < -999999999999)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ internal class BulgarianNumberToWordsConverter : GenderedNumberToWordsConverter
"осемнадесет", "деветнадесет"
};

public override string Convert(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
return Convert(input, gender, false);
}

private string Convert(long input, GrammaticalGender gender, bool isOrdinal)
private string Convert(long input, GrammaticalGender gender, bool isOrdinal, bool addAnd = true)
{
if (input > int.MaxValue || input < int.MinValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public CzechNumberToWordsConverter(CultureInfo culture)
_culture = culture;
}

public override string Convert(long number, GrammaticalGender gender)
public override string Convert(long number, GrammaticalGender gender, bool addAnd = true)
{
if (number == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ public override string Convert(long number)
return Convert(number, false);
}

public override string Convert(long number, bool addAnd = true)
{
return Convert(number, false, addAnd);
}

public override string ConvertToOrdinal(int number)
{
return Convert(number, true);
}

private string Convert(long number, bool isOrdinal)
private string Convert(long number, bool isOrdinal, bool addAnd = true)
{
if (number == 0)
{
Expand Down Expand Up @@ -88,7 +93,7 @@ private string Convert(long number, bool isOrdinal)

if (number > 0)
{
if (parts.Count != 0)
if (parts.Count != 0 && addAnd)
{
parts.Add("and");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal abstract class FrenchNumberToWordsConverterBase : GenderedNumberToWords
private static readonly string[] UnitsMap = { "zéro", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix", "onze", "douze", "treize", "quatorze", "quinze", "seize", "dix-sept", "dix-huit", "dix-neuf" };
private static readonly string[] TensMap = { "zéro", "dix", "vingt", "trente", "quarante", "cinquante", "soixante", "septante", "octante", "nonante" };

public override string Convert(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ public string Convert(long number)
return Convert(number, _defaultGender);
}

public string Convert(long number, bool addAnd)
{
return Convert(number, _defaultGender);
}

/// <summary>
/// Converts the number to string using the provided grammatical gender
/// </summary>
/// <param name="number"></param>
/// <param name="gender"></param>
/// <returns></returns>
public abstract string Convert(long number, GrammaticalGender gender);
public abstract string Convert(long number, GrammaticalGender gender, bool addAnd = true);


/// <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 @@ -9,17 +9,23 @@ internal abstract class GenderlessNumberToWordsConverter : INumberToWordsConvert
/// <returns></returns>
public abstract string Convert(long number);

public virtual string Convert(long number, bool addAnd)
{
return Convert(number);
}

/// <summary>
/// Converts the number to string ignoring the provided grammatical gender
/// </summary>
/// <param name="number"></param>
/// <param name="gender"></param>
/// <returns></returns>
public virtual string Convert(long number, GrammaticalGender gender)
public virtual string Convert(long number, GrammaticalGender gender, bool addAnd = true)
{
return Convert(number);
}


/// <summary>
/// Converts the number to ordinal string
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal abstract class GermanNumberToWordsConverterBase : GenderedNumberToWords
private readonly string[] BillionOrdinalSingular = { "einmilliard", "einemilliarde" };
private readonly string[] BillionOrdinalPlural = { "{0}milliard", "{0}milliarden" };

public override string Convert(long number, GrammaticalGender gender)
public override string Convert(long number, GrammaticalGender gender, bool addAnd = true)
{
if (number == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public HebrewNumberToWordsConverter(CultureInfo culture)
_culture = culture;
}

public override string Convert(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ public interface INumberToWordsConverter
/// <returns></returns>
string Convert(long number);

/// <summary>
/// Converts the number to string using the locale's default grammatical gender with or without adding 'And'
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
string Convert(long number, bool addAnd);

/// <summary>
/// Converts the number to string using the provided grammatical gender
/// </summary>
/// <param name="number"></param>
/// <param name="gender"></param>
/// <returns></returns>
string Convert(long number, GrammaticalGender gender);
string Convert(long number, GrammaticalGender gender, bool addAnd = true);

/// <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 @@ -5,7 +5,7 @@ namespace Humanizer.Localisation.NumberToWords
{
internal class ItalianNumberToWordsConverter : GenderedNumberToWordsConverter
{
public override string Convert(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class MalteseNumberToWordsConvertor : GenderedNumberToWordsConverter
"tmintax-il", "dsatax-il"
};

public override string Convert(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
bool negativeNumber = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class NorwegianBokmalNumberToWordsConverter : GenderedNumberToWordsConv
{12, "tolvte"}
};

public override string Convert(long number, GrammaticalGender gender)
public override string Convert(long number, GrammaticalGender gender, bool addAnd = true)
{
if (number > Int32.MaxValue || number < Int32.MinValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class PortugueseNumberToWordsConverter : GenderedNumberToWordsConverter
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(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
if (input > 999999999999 || input < -999999999999)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Humanizer.Localisation.NumberToWords
{
internal class RomanianNumberToWordsConverter : GenderedNumberToWordsConverter
{
public override string Convert(long number, GrammaticalGender gender)
public override string Convert(long number, GrammaticalGender gender, bool addAnd = true)
{
if (number > Int32.MaxValue || number < Int32.MinValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class RussianNumberToWordsConverter : GenderedNumberToWordsConverter
private static readonly string[] TensOrdinal = { string.Empty, "десят", "двадцат", "тридцат", "сороков", "пятидесят", "шестидесят", "семидесят", "восьмидесят", "девяност" };
private static readonly string[] UnitsOrdinal = { string.Empty, "перв", "втор", "трет", "четверт", "пят", "шест", "седьм", "восьм", "девят", "десят", "одиннадцат", "двенадцат", "тринадцат", "четырнадцат", "пятнадцат", "шестнадцат", "семнадцат", "восемнадцат", "девятнадцат" };

public override string Convert(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
if (input == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class SpanishNumberToWordsConverter : GenderedNumberToWordsConverter
{9, "noveno"},
};

public override string Convert(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ private class Fact
new Fact {Value = 1000, Name = "tusen", Prefix = " ", Postfix = " ", DisplayOneUnit = true},
new Fact {Value = 100, Name = "hundra", Prefix = "", Postfix = "", DisplayOneUnit = false}
};

public override string Convert(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
if (input > Int32.MaxValue || input < Int32.MinValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class UkrainianNumberToWordsConverter : GenderedNumberToWordsConverter
private static readonly string[] TensOrdinal = { string.Empty, "десят", "двадцят", "тридцят", "сороков", "п'ятдесят", "шістдесят", "сімдесят", "вісімдесят", "дев'яност" };
private static readonly string[] UnitsOrdinal = { "нульов", "перш", "друг", "трет", "четверт", "п'ят", "шост", "сьом", "восьм", "дев'ят", "десят", "одинадцят", "дванадцят", "тринадцят", "чотирнадцят", "п'ятнадцят", "шістнадцят", "сімнадцят", "вісімнадцят", "дев'ятнадцят" };

public override string Convert(long input, GrammaticalGender gender)
public override string Convert(long input, GrammaticalGender gender, bool addAnd = true)
{
if (input == 0)
{
Expand Down
17 changes: 15 additions & 2 deletions src/Humanizer/NumberToWordsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ public static string ToWords(this int number, CultureInfo culture = null)
return ((long)number).ToWords(culture);
}


/// <summary>
/// 3501.ToWords(false) -> "three thousand five hundred one"
/// </summary>
/// <param name="number">Number to be turned to words</param>
/// <param name="addAnd">To add 'and' before the last number.</param>
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <returns></returns>
public static string ToWords(this int number,bool addAnd, CultureInfo culture = null)
{
return ((long)number).ToWords(culture, addAnd);
}

/// <summary>
/// For locales that support gender-specific forms
/// </summary>
Expand Down Expand Up @@ -50,9 +63,9 @@ public static string ToWords(this int number, GrammaticalGender gender, CultureI
/// <param name="number">Number to be turned to words</param>
/// <param name="culture">Culture to use. If null, current thread's UI culture is used.</param>
/// <returns></returns>
public static string ToWords(this long number, CultureInfo culture = null)
public static string ToWords(this long number, CultureInfo culture = null, bool addAnd = true)
{
return Configurator.GetNumberToWordsConverter(culture).Convert(number);
return Configurator.GetNumberToWordsConverter(culture).Convert(number, addAnd);
}

/// <summary>
Expand Down

0 comments on commit c5d77ba

Please sign in to comment.