From 88c7bcb902d627dd4812b76c6d6d1736774e8f47 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 23 Feb 2024 16:01:01 +1100 Subject: [PATCH] remove redundant number variables from LatvianNumberToWordsConverter (#1440) --- .../LatvianNumberToWordsConverter.cs | 352 +++++++++--------- 1 file changed, 174 insertions(+), 178 deletions(-) diff --git a/src/Humanizer/Localisation/NumberToWords/LatvianNumberToWordsConverter.cs b/src/Humanizer/Localisation/NumberToWords/LatvianNumberToWordsConverter.cs index ac95a7eb9..edebe3633 100644 --- a/src/Humanizer/Localisation/NumberToWords/LatvianNumberToWordsConverter.cs +++ b/src/Humanizer/Localisation/NumberToWords/LatvianNumberToWordsConverter.cs @@ -1,240 +1,236 @@ -namespace Humanizer +namespace Humanizer; + +class LatvianNumberToWordsConverter : GenderedNumberToWordsConverter { - class LatvianNumberToWordsConverter : GenderedNumberToWordsConverter + static readonly string[] UnitsMap = ["nulle", "vien", "div", "trīs", "četr", "piec", "seš", "septiņ", "astoņ", "deviņ", "desmit", "vienpadsmit", "divpadsmit", "trīspadsmit", "četrpadsmit", "piecpadsmit", "sešpadsmit", "septiņpadsmit", "astoņpadsmit", "deviņpadsmit"]; + static readonly string[] TensMap = ["nulle", "desmit", "divdesmit", "trīsdesmit", "četrdesmit", "piecdesmit", "sešdesmit", "septiņdesmit", "astoņdesmit", "deviņdesmit"]; + static readonly string[] HundredsMap = ["nulle", "simt", "divsimt", "trīssimt", "četrsimt", "piecsimt", "sešsimt", "septiņsimt", "astoņsimt", "deviņsimt"]; + static readonly string[] UnitsOrdinal = [string.Empty, "pirm", "otr", "treš", "ceturt", "piekt", "sest", "septīt", "astot", "devīt", "desmit", "vienpadsmit", "divpadsmit", "trīspadsmit", "četrpadsmit", "piecpadsmit", "sešpadsmit", "septiņpadsmit", "astoņpadsmit", "deviņpadsmit", "divdesmit"]; + + public override string Convert(long input, GrammaticalGender gender, bool addAnd = true) { - static readonly string[] UnitsMap = ["nulle", "vien", "div", "trīs", "četr", "piec", "seš", "septiņ", "astoņ", "deviņ", "desmit", "vienpadsmit", "divpadsmit", "trīspadsmit", "četrpadsmit", "piecpadsmit", "sešpadsmit", "septiņpadsmit", "astoņpadsmit", "deviņpadsmit"]; - static readonly string[] TensMap = ["nulle", "desmit", "divdesmit", "trīsdesmit", "četrdesmit", "piecdesmit", "sešdesmit", "septiņdesmit", "astoņdesmit", "deviņdesmit"]; - static readonly string[] HundredsMap = ["nulle", "simt", "divsimt", "trīssimt", "četrsimt", "piecsimt", "sešsimt", "septiņsimt", "astoņsimt", "deviņsimt"]; - static readonly string[] UnitsOrdinal = [string.Empty, "pirm", "otr", "treš", "ceturt", "piekt", "sest", "septīt", "astot", "devīt", "desmit", "vienpadsmit", "divpadsmit", "trīspadsmit", "četrpadsmit", "piecpadsmit", "sešpadsmit", "septiņpadsmit", "astoņpadsmit", "deviņpadsmit", "divdesmit"]; + if (input is > int.MaxValue or < int.MinValue) + { + throw new NotImplementedException(); + } + + var parts = new List(); - public override string Convert(long input, GrammaticalGender gender, bool addAnd = true) + if (input / 1000000 > 0) { - if (input is > int.MaxValue or < int.MinValue) + string millionPart; + if (input == 1000000) { - throw new NotImplementedException(); + millionPart = "miljons"; } - - var parts = new List(); - var number = (long)input; - - if (number / 1000000 > 0) + else { - string millionPart; - if (number == 1000000) - { - millionPart = "miljons"; - } - else - { - millionPart = Convert(number / 1000000, GrammaticalGender.Masculine) + " miljoni"; - } - number %= 1000000; - parts.Add(millionPart); + millionPart = Convert(input / 1000000, GrammaticalGender.Masculine) + " miljoni"; } + input %= 1000000; + parts.Add(millionPart); + } - if (number / 1000 > 0) + if (input / 1000 > 0) + { + string thousandsPart; + if (input == 1000) { - string thousandsPart; - if (number == 1000) - { - thousandsPart = "tūkstotis"; - } - else if (number is > 1000 and < 2000) - { - thousandsPart = "tūkstoš"; - } - else - { - thousandsPart = Convert(number / 1000, GrammaticalGender.Masculine) + " tūkstoši"; - } - parts.Add(thousandsPart); - number %= 1000; + thousandsPart = "tūkstotis"; } - - if (number / 100 > 0) + else if (input is > 1000 and < 2000) { - string hundredsPart; - if (number == 100) - { - hundredsPart = parts.Contains("tūkstoš") ? "viens simts" : "simts"; - } - else if (number is > 100 and < 200) - { - hundredsPart = "simtu"; - } - else - { - hundredsPart = Convert(number / 100, GrammaticalGender.Masculine) + " simti"; - } - parts.Add(hundredsPart); - number %= 100; + thousandsPart = "tūkstoš"; } - - if (number > 19) + else { - var tensPart = TensMap[number / 10]; - parts.Add(tensPart); - number %= 10; + thousandsPart = Convert(input / 1000, GrammaticalGender.Masculine) + " tūkstoši"; } + parts.Add(thousandsPart); + input %= 1000; + } - if (number > 0) + if (input / 100 > 0) + { + string hundredsPart; + if (input == 100) + { + hundredsPart = parts.Contains("tūkstoš") ? "viens simts" : "simts"; + } + else if (input is > 100 and < 200) { - parts.Add(UnitsMap[number] + GetCardinalEndingForGender(gender, number)); + hundredsPart = "simtu"; } + else + { + hundredsPart = Convert(input / 100, GrammaticalGender.Masculine) + " simti"; + } + parts.Add(hundredsPart); + input %= 100; + } - return string.Join(" ", parts); + if (input > 19) + { + var tensPart = TensMap[input / 10]; + parts.Add(tensPart); + input %= 10; } - public override string ConvertToOrdinal(int input, GrammaticalGender gender) + if (input > 0) { - if (input == 0) - { - return "nulle"; - } + parts.Add(UnitsMap[input] + GetCardinalEndingForGender(gender, input)); + } + + return string.Join(" ", parts); + } - var parts = new List(); + public override string ConvertToOrdinal(int input, GrammaticalGender gender) + { + if (input == 0) + { + return "nulle"; + } - if (input < 0) + var parts = new List(); + + if (input < 0) + { + parts.Add("mīnus"); + input = -input; + } + + if (input / 1000000 > 0) + { + string millionPart; + if (input == 1000000) { - parts.Add("mīnus"); - input = -input; + millionPart = "miljon" + GetOrdinalEndingForGender(gender); } + else + { + millionPart = Convert(input / 1000000, GrammaticalGender.Masculine) + " miljon" + GetOrdinalEndingForGender(gender); + } + input %= 1000000; + parts.Add(millionPart); + } - var number = (long)input; - - if (number / 1000000 > 0) + if (input / 1000 > 0) + { + string thousandsPart; + if (input % 1000 == 0) { - string millionPart; - if (number == 1000000) + if (input == 1000) { - millionPart = "miljon" + GetOrdinalEndingForGender(gender); + thousandsPart = "tūkstoš" + GetOrdinalEndingForGender(gender); } else { - millionPart = Convert(number / 1000000, GrammaticalGender.Masculine) + " miljon" + GetOrdinalEndingForGender(gender); + thousandsPart = Convert(input / 1000, GrammaticalGender.Masculine) + " tūkstoš" + GetOrdinalEndingForGender(gender); } - number %= 1000000; - parts.Add(millionPart); } - - if (number / 1000 > 0) + else { - string thousandsPart; - if (number % 1000 == 0) + if (input is > 1000 and < 2000) { - if (number == 1000) - { - thousandsPart = "tūkstoš" + GetOrdinalEndingForGender(gender); - } - else - { - thousandsPart = Convert(number / 1000, GrammaticalGender.Masculine) + " tūkstoš" + GetOrdinalEndingForGender(gender); - } + thousandsPart = "tūkstoš"; } else { - if (number is > 1000 and < 2000) - { - thousandsPart = "tūkstoš"; - } - else - { - thousandsPart = Convert(number / 1000, GrammaticalGender.Masculine) + " tūkstoši"; - } + thousandsPart = Convert(input / 1000, GrammaticalGender.Masculine) + " tūkstoši"; } - parts.Add(thousandsPart); - number %= 1000; } + parts.Add(thousandsPart); + input %= 1000; + } - if (number / 100 > 0) + if (input / 100 > 0) + { + string hundredsPart; + if (input % 100 == 0) + { + hundredsPart = HundredsMap[input / 100] + GetOrdinalEndingForGender(gender); + } + else { - string hundredsPart; - if (number % 100 == 0) + if (input is > 100 and < 200) { - hundredsPart = HundredsMap[number / 100] + GetOrdinalEndingForGender(gender); + hundredsPart = "simtu"; } else { - if (number is > 100 and < 200) - { - hundredsPart = "simtu"; - } - else - { - hundredsPart = Convert(number / 100, GrammaticalGender.Masculine) + " simti"; - } - } - parts.Add(hundredsPart); - number %= 100; - } - - if (number > 19) - { - var tensPart = TensMap[number / 10]; - if (number % 10 == 0) - { - tensPart += GetOrdinalEndingForGender(gender); + hundredsPart = Convert(input / 100, GrammaticalGender.Masculine) + " simti"; } - parts.Add(tensPart); - number %= 10; } + parts.Add(hundredsPart); + input %= 100; + } - if (number > 0) + if (input > 19) + { + var tensPart = TensMap[input / 10]; + if (input % 10 == 0) { - parts.Add(UnitsOrdinal[number] + GetOrdinalEndingForGender(gender)); + tensPart += GetOrdinalEndingForGender(gender); } + parts.Add(tensPart); + input %= 10; + } - return string.Join(" ", parts); + if (input > 0) + { + parts.Add(UnitsOrdinal[input] + GetOrdinalEndingForGender(gender)); } - static string GetOrdinalEndingForGender(GrammaticalGender gender) + return string.Join(" ", parts); + } + + static string GetOrdinalEndingForGender(GrammaticalGender gender) + { + switch (gender) { - switch (gender) - { - case GrammaticalGender.Masculine: - { - return "ais"; - } - case GrammaticalGender.Feminine: - { - return "ā"; - } - default: - throw new ArgumentOutOfRangeException(nameof(gender)); + case GrammaticalGender.Masculine: + { + return "ais"; } + case GrammaticalGender.Feminine: + { + return "ā"; + } + default: + throw new ArgumentOutOfRangeException(nameof(gender)); } + } - static string GetCardinalEndingForGender(GrammaticalGender gender, long number) + static string GetCardinalEndingForGender(GrammaticalGender gender, long number) + { + switch (gender) { - switch (gender) - { - case GrammaticalGender.Masculine: - if (number == 1) - { - return "s"; - } - - if (number != 3 && number < 10) - { - return "i"; - } - - return ""; - case GrammaticalGender.Feminine: - if (number == 1) - { - return "a"; - } - - if (number != 3 && number < 10) - { - return "as"; - } - - return ""; - default: - throw new ArgumentOutOfRangeException(nameof(gender)); - } + case GrammaticalGender.Masculine: + if (number == 1) + { + return "s"; + } + + if (number != 3 && number < 10) + { + return "i"; + } + + return ""; + case GrammaticalGender.Feminine: + if (number == 1) + { + return "a"; + } + + if (number != 3 && number < 10) + { + return "as"; + } + + return ""; + default: + throw new ArgumentOutOfRangeException(nameof(gender)); } } -} +} \ No newline at end of file