From 8f762c8776ddc06ccead6d43584ec9be197f7d2e Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 7 Aug 2024 23:30:31 +0200 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB273.=20Integer=20to=20English?= =?UTF-8?q?=20Words=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/integer-to-english-words.cs | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 cs/integer-to-english-words.cs diff --git a/cs/integer-to-english-words.cs b/cs/integer-to-english-words.cs new file mode 100644 index 0000000..ea8827e --- /dev/null +++ b/cs/integer-to-english-words.cs @@ -0,0 +1,51 @@ +public class Solution { + private static readonly List<(int value, string word)> NUMBERS = new List<(int value, string word)>() { + (1000000000, "Billion"), + (1000000, "Million"), + (1000, "Thousand"), + (100, "Hundred"), + (90, "Ninety"), + (80, "Eighty"), + (70, "Seventy"), + (60, "Sixty"), + (50, "Fifty"), + (40, "Forty"), + (30, "Thirty"), + (20, "Twenty"), + (19, "Nineteen"), + (18, "Eighteen"), + (17, "Seventeen"), + (16, "Sixteen"), + (15, "Fifteen"), + (14, "Fourteen"), + (13, "Thirteen"), + (12, "Twelve"), + (11, "Eleven"), + (10, "Ten"), + (9, "Nine"), + (8, "Eight"), + (7, "Seven"), + (6, "Six"), + (5, "Five"), + (4, "Four"), + (3, "Three"), + (2, "Two"), + (1, "One"), + }; + + public string NumberToWords(int num) { + if (num == 0) { + return "Zero"; + } + + foreach (var (value, word) in NUMBERS) { + if (num >= value) { + var prefix = (num >= 100) ? NumberToWords(num / value) + " " : ""; + var suffix = (num % value == 0) ? "" : " " + NumberToWords(num % value); + return prefix + word + suffix; + } + } + + return ""; + } +}