diff --git a/readme.md b/readme.md index 50915b3aa..d94b52fc6 100644 --- a/readme.md +++ b/readme.md @@ -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. + ### Number to ordinal words 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: diff --git a/src/Humanizer.Tests.Shared/NumberToWordsTests.cs b/src/Humanizer.Tests.Shared/NumberToWordsTests.cs index 9e6ceaeae..675efee17 100644 --- a/src/Humanizer.Tests.Shared/NumberToWordsTests.cs +++ b/src/Humanizer.Tests.Shared/NumberToWordsTests.cs @@ -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")] diff --git a/src/Humanizer/Localisation/NumberToWords/ArabicNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/ArabicNumberToWordsConverter.cs index ae7f5f3a3..51576cf80 100644 --- a/src/Humanizer/Localisation/NumberToWords/ArabicNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/ArabicNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/BrazilianPortugueseNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/BrazilianPortugueseNumberToWordsConverter.cs index dca55b26f..7f4f101c3 100644 --- a/src/Humanizer/Localisation/NumberToWords/BrazilianPortugueseNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/BrazilianPortugueseNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/BulgarianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/BulgarianNumberToWordsConverter.cs index 57d78410e..e6a908f5f 100644 --- a/src/Humanizer/Localisation/NumberToWords/BulgarianNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/BulgarianNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/CzechNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/CzechNumberToWordsConverter.cs index 454c4464d..9c190fef9 100644 --- a/src/Humanizer/Localisation/NumberToWords/CzechNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/CzechNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs index 0c81b7ae8..e2b50167c 100644 --- a/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs @@ -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) { @@ -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"); } diff --git a/src/Humanizer/Localisation/NumberToWords/FrenchNumberToWordsConverterBase.cs b/src/Humanizer/Localisation/NumberToWords/FrenchNumberToWordsConverterBase.cs index 65c0c790a..8ac9832f3 100644 --- a/src/Humanizer/Localisation/NumberToWords/FrenchNumberToWordsConverterBase.cs +++ b/src/Humanizer/Localisation/NumberToWords/FrenchNumberToWordsConverterBase.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/GenderedNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/GenderedNumberToWordsConverter.cs index 86f1a24cb..093b00144 100644 --- a/src/Humanizer/Localisation/NumberToWords/GenderedNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/GenderedNumberToWordsConverter.cs @@ -19,13 +19,19 @@ public string Convert(long number) return Convert(number, _defaultGender); } + public string Convert(long number, bool addAnd) + { + return Convert(number, _defaultGender); + } + /// /// Converts the number to string using the provided grammatical gender /// /// /// /// - public abstract string Convert(long number, GrammaticalGender gender); + public abstract string Convert(long number, GrammaticalGender gender, bool addAnd = true); + /// /// Converts the number to ordinal string using the locale's default grammatical gender diff --git a/src/Humanizer/Localisation/NumberToWords/GenderlessNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/GenderlessNumberToWordsConverter.cs index e7c4fe529..87e651cff 100644 --- a/src/Humanizer/Localisation/NumberToWords/GenderlessNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/GenderlessNumberToWordsConverter.cs @@ -9,17 +9,23 @@ internal abstract class GenderlessNumberToWordsConverter : INumberToWordsConvert /// public abstract string Convert(long number); + public virtual string Convert(long number, bool addAnd) + { + return Convert(number); + } + /// /// Converts the number to string ignoring the provided grammatical gender /// /// /// /// - public virtual string Convert(long number, GrammaticalGender gender) + public virtual string Convert(long number, GrammaticalGender gender, bool addAnd = true) { return Convert(number); } + /// /// Converts the number to ordinal string /// diff --git a/src/Humanizer/Localisation/NumberToWords/GermanNumberToWordsConverterBase.cs b/src/Humanizer/Localisation/NumberToWords/GermanNumberToWordsConverterBase.cs index aef3c062b..c21ba1d8d 100644 --- a/src/Humanizer/Localisation/NumberToWords/GermanNumberToWordsConverterBase.cs +++ b/src/Humanizer/Localisation/NumberToWords/GermanNumberToWordsConverterBase.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/HebrewNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/HebrewNumberToWordsConverter.cs index 92f7857cb..7ecda3774 100644 --- a/src/Humanizer/Localisation/NumberToWords/HebrewNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/HebrewNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/INumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/INumberToWordsConverter.cs index 151770be1..19033d15f 100644 --- a/src/Humanizer/Localisation/NumberToWords/INumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/INumberToWordsConverter.cs @@ -12,13 +12,20 @@ public interface INumberToWordsConverter /// string Convert(long number); + /// + /// Converts the number to string using the locale's default grammatical gender with or without adding 'And' + /// + /// + /// + string Convert(long number, bool addAnd); + /// /// Converts the number to string using the provided grammatical gender /// /// /// /// - string Convert(long number, GrammaticalGender gender); + string Convert(long number, GrammaticalGender gender, bool addAnd = true); /// /// Converts the number to ordinal string using the locale's default grammatical gender diff --git a/src/Humanizer/Localisation/NumberToWords/ItalianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/ItalianNumberToWordsConverter.cs index e3ac780bc..29c82d1a9 100644 --- a/src/Humanizer/Localisation/NumberToWords/ItalianNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/ItalianNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/MalteseNumberToWordsConvertor.cs b/src/Humanizer/Localisation/NumberToWords/MalteseNumberToWordsConvertor.cs index 9237e9ccf..b872432b5 100644 --- a/src/Humanizer/Localisation/NumberToWords/MalteseNumberToWordsConvertor.cs +++ b/src/Humanizer/Localisation/NumberToWords/MalteseNumberToWordsConvertor.cs @@ -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; diff --git a/src/Humanizer/Localisation/NumberToWords/NorwegianBokmalNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/NorwegianBokmalNumberToWordsConverter.cs index baa730059..c94b85e2a 100644 --- a/src/Humanizer/Localisation/NumberToWords/NorwegianBokmalNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/NorwegianBokmalNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/PortugueseNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/PortugueseNumberToWordsConverter.cs index 553c1158b..9e7daa298 100644 --- a/src/Humanizer/Localisation/NumberToWords/PortugueseNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/PortugueseNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/RomanianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/RomanianNumberToWordsConverter.cs index f6071cf96..76524171c 100644 --- a/src/Humanizer/Localisation/NumberToWords/RomanianNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/RomanianNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/RussianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/RussianNumberToWordsConverter.cs index 1375cd5b8..50b870a72 100644 --- a/src/Humanizer/Localisation/NumberToWords/RussianNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/RussianNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs index 441b89ce5..f89f25959 100644 --- a/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/SpanishNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/SwedishNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/SwedishNumberToWordsConverter.cs index b5c150810..325c3420a 100644 --- a/src/Humanizer/Localisation/NumberToWords/SwedishNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/SwedishNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/Localisation/NumberToWords/UkrainianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/UkrainianNumberToWordsConverter.cs index 87d05d6db..0ca5e6b7d 100644 --- a/src/Humanizer/Localisation/NumberToWords/UkrainianNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/UkrainianNumberToWordsConverter.cs @@ -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) { diff --git a/src/Humanizer/NumberToWordsExtension.cs b/src/Humanizer/NumberToWordsExtension.cs index cfcb189fb..b93f2d0cc 100644 --- a/src/Humanizer/NumberToWordsExtension.cs +++ b/src/Humanizer/NumberToWordsExtension.cs @@ -19,6 +19,19 @@ public static string ToWords(this int number, CultureInfo culture = null) return ((long)number).ToWords(culture); } + + /// + /// 3501.ToWords(false) -> "three thousand five hundred one" + /// + /// Number to be turned to words + /// To add 'and' before the last number. + /// Culture to use. If null, current thread's UI culture is used. + /// + public static string ToWords(this int number,bool addAnd, CultureInfo culture = null) + { + return ((long)number).ToWords(culture, addAnd); + } + /// /// For locales that support gender-specific forms /// @@ -50,9 +63,9 @@ public static string ToWords(this int number, GrammaticalGender gender, CultureI /// Number to be turned to words /// Culture to use. If null, current thread's UI culture is used. /// - 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); } ///