Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
NumberToWords C# Structural
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public static class TestNumberToWordsHelpers{ public static bool ShouldBeSpelledAs(this string result, string expected, bool showPassed = false){ if(result != expected) Console.WriteLine("|FAILED| Expected result: " + expected + ", actual result: " + result + "" ); else if(showPassed) Console.WriteLine("PASSED: " + result + " is as expected: [" + expected + "] :)" ); return result == expected; } } public static class Program { public static string NtWUnits(int i){ switch(i) { case 0: //Exceptional case, added in the method NumberToWords return ""; case 1: return "One"; case 2: return "Two"; case 3: return "Three"; case 4: return "Four"; case 5: return "Five"; case 6: return "Six"; case 7: return "Seven"; case 8: return "Eight"; case 9: return "Nine"; } return "Undefined Unit_0_9: " + i; } public static string NtW_Tens_10_19(int i){ switch(i) { case 0: return "Ten"; case 1: return "Eleven"; case 2: return "Twelve"; case 3: return "Thirteen"; case 4: return "Fourteen"; case 5: return "Fifteen"; case 6: return "Sixteen"; case 7: return "Seventeen"; case 8: return "Eighteen"; case 9: return "Nineteen"; } return "Undefined Ten_10_19: " + i; } public static string NtWTens(int i){ if(i > 99) return ""; string tTxt = ""; string uTxt = ""; int t = i / 10; int u = (i -(t*10)) % 10; if(t == 1) { tTxt = NtW_Tens_10_19(u); } else { if(t >= 2 && t <= 9) { switch(t) { case 2: tTxt = "Twenty"; break; case 3: tTxt = "Thirty"; break; case 4: tTxt = "Forty"; break; case 5: tTxt = "Fifty"; break; case 6: tTxt = "Sixty"; break; case 7: tTxt = "Seventy"; break; case 8: tTxt = "Eighty"; break; case 9: tTxt = "Ninety"; break; default: tTxt = "Undefined Ten!: " + i; break; } tTxt = tTxt + ((u > 0) ? "-" : ""); } if(u >= 0 && u <= 9) uTxt = NtWUnits(u); } return tTxt + uTxt; } public static string NtWHundreds(int i, string hundredsTextSeparator = " and "){ if(i > 999) return ""; string hTxt = ""; int h = i / 100; int t = (i-(h*100)) / 10; int u = (i-(h*100)) % 10; if(h > 0) { hTxt = NtWUnits(h) + " hundred" + (t > 0 || u > 0 ? hundredsTextSeparator : ""); } return hTxt + NtWTens(i-(h*100)); } public static string NtWThousands(int i, string separator = " and ") { if(i > 999999) return ""; string thTxt = ""; string aux = ""; int th = i / 1000; if(th > 0 && th < 1000){ thTxt = NtWHundreds(th, " ") + " thousand"; } aux = NtWHundreds(i - (th*1000)); return thTxt + (string.IsNullOrEmpty(aux) ? "" : (th > 0 ? separator + aux : aux)); } public static string NtWMillions(int i) { if(i > 999999999) return ""; string mTxt = ""; string aux = ""; int m = i / 1000000; if(m > 0 && m < 1000){ mTxt = NtWHundreds(m, " ") + " million"; } aux = NtWThousands(i - (m*1000000), m > 0 ? " " : " and "); return mTxt + (string.IsNullOrEmpty(aux) ? "" : (m > 0 ? " " + aux : aux)); } public static string ToWords(this int i) { if(i == 0) return "Zero"; return NtWMillions(i); } public static void TestUnits(){ //First, Unique and simple cases. 0.ToWords().ShouldBeSpelledAs("Zero"); 1.ToWords().ShouldBeSpelledAs("One"); 2.ToWords().ShouldBeSpelledAs("Two"); 3.ToWords().ShouldBeSpelledAs("Three"); 4.ToWords().ShouldBeSpelledAs("Four"); 5.ToWords().ShouldBeSpelledAs("Five"); 6.ToWords().ShouldBeSpelledAs("Six"); 7.ToWords().ShouldBeSpelledAs("Seven"); 8.ToWords().ShouldBeSpelledAs("Eight"); 9.ToWords().ShouldBeSpelledAs("Nine"); } public static void TestTens_10_19(){ 10.ToWords().ShouldBeSpelledAs("Ten"); 11.ToWords().ShouldBeSpelledAs("Eleven"); 12.ToWords().ShouldBeSpelledAs("Twelve"); 13.ToWords().ShouldBeSpelledAs("Thirteen"); 14.ToWords().ShouldBeSpelledAs("Fourteen"); 15.ToWords().ShouldBeSpelledAs("Fifteen"); 16.ToWords().ShouldBeSpelledAs("Sixteen"); 17.ToWords().ShouldBeSpelledAs("Seventeen"); 18.ToWords().ShouldBeSpelledAs("Eighteen"); 19.ToWords().ShouldBeSpelledAs("Nineteen"); } public static void TestTens_20_99(){ //Test of 20-29 subset 20.ToWords().ShouldBeSpelledAs("Twenty"); 21.ToWords().ShouldBeSpelledAs("Twenty-One"); 22.ToWords().ShouldBeSpelledAs("Twenty-Two"); 23.ToWords().ShouldBeSpelledAs("Twenty-Three"); 24.ToWords().ShouldBeSpelledAs("Twenty-Four"); 25.ToWords().ShouldBeSpelledAs("Twenty-Five"); 26.ToWords().ShouldBeSpelledAs("Twenty-Six"); 27.ToWords().ShouldBeSpelledAs("Twenty-Seven"); 28.ToWords().ShouldBeSpelledAs("Twenty-Eight"); 29.ToWords().ShouldBeSpelledAs("Twenty-Nine"); //Test 30-39 subset. Use boundary testing (https://en.wikipedia.org/wiki/Boundary_testing) 30.ToWords().ShouldBeSpelledAs("Thirty"); 31.ToWords().ShouldBeSpelledAs("Thirty-One"); 39.ToWords().ShouldBeSpelledAs("Thirty-Nine"); 40.ToWords().ShouldBeSpelledAs("Forty"); 41.ToWords().ShouldBeSpelledAs("Forty-One"); 49.ToWords().ShouldBeSpelledAs("Forty-Nine"); 50.ToWords().ShouldBeSpelledAs("Fifty"); 51.ToWords().ShouldBeSpelledAs("Fifty-One"); 59.ToWords().ShouldBeSpelledAs("Fifty-Nine"); 60.ToWords().ShouldBeSpelledAs("Sixty"); 61.ToWords().ShouldBeSpelledAs("Sixty-One"); 69.ToWords().ShouldBeSpelledAs("Sixty-Nine"); 70.ToWords().ShouldBeSpelledAs("Seventy"); 71.ToWords().ShouldBeSpelledAs("Seventy-One"); 79.ToWords().ShouldBeSpelledAs("Seventy-Nine"); 80.ToWords().ShouldBeSpelledAs("Eighty"); 81.ToWords().ShouldBeSpelledAs("Eighty-One"); 89.ToWords().ShouldBeSpelledAs("Eighty-Nine"); 90.ToWords().ShouldBeSpelledAs("Ninety"); 91.ToWords().ShouldBeSpelledAs("Ninety-One"); 99.ToWords().ShouldBeSpelledAs("Ninety-Nine"); } public static void TestHundred_100_999(){ //Test of 100-199 subset. Test bases of 100 (100, 200, etc) and border of different subsets (0 and 9 for units, 10, 19 for tens, 20 - 29...) 100.ToWords().ShouldBeSpelledAs("One hundred"); 101.ToWords().ShouldBeSpelledAs("One hundred and One"); 109.ToWords().ShouldBeSpelledAs("One hundred and Nine"); 110.ToWords().ShouldBeSpelledAs("One hundred and Ten"); 119.ToWords().ShouldBeSpelledAs("One hundred and Nineteen"); 120.ToWords().ShouldBeSpelledAs("One hundred and Twenty"); 129.ToWords().ShouldBeSpelledAs("One hundred and Twenty-Nine"); 190.ToWords().ShouldBeSpelledAs("One hundred and Ninety"); 199.ToWords().ShouldBeSpelledAs("One hundred and Ninety-Nine"); 200.ToWords().ShouldBeSpelledAs("Two hundred"); 201.ToWords().ShouldBeSpelledAs("Two hundred and One"); 209.ToWords().ShouldBeSpelledAs("Two hundred and Nine"); 210.ToWords().ShouldBeSpelledAs("Two hundred and Ten"); 219.ToWords().ShouldBeSpelledAs("Two hundred and Nineteen"); 220.ToWords().ShouldBeSpelledAs("Two hundred and Twenty"); 229.ToWords().ShouldBeSpelledAs("Two hundred and Twenty-Nine"); 290.ToWords().ShouldBeSpelledAs("Two hundred and Ninety"); 299.ToWords().ShouldBeSpelledAs("Two hundred and Ninety-Nine"); 900.ToWords().ShouldBeSpelledAs("Nine hundred"); 999.ToWords().ShouldBeSpelledAs("Nine hundred and Ninety-Nine"); } public static void TestThousands() { 1000.ToWords().ShouldBeSpelledAs("One thousand"); 1001.ToWords().ShouldBeSpelledAs("One thousand and One"); 1010.ToWords().ShouldBeSpelledAs("One thousand and Ten"); 1011.ToWords().ShouldBeSpelledAs("One thousand and Eleven"); 1024.ToWords().ShouldBeSpelledAs("One thousand and Twenty-Four"); 1100.ToWords().ShouldBeSpelledAs("One thousand and One hundred"); 1101.ToWords().ShouldBeSpelledAs("One thousand and One hundred and One"); 1728.ToWords().ShouldBeSpelledAs("One thousand and Seven hundred and Twenty-Eight"); 101101.ToWords().ShouldBeSpelledAs("One hundred One thousand and One hundred and One"); 999999.ToWords().ShouldBeSpelledAs("Nine hundred Ninety-Nine thousand and Nine hundred and Ninety-Nine"); } public static void TestMillions() { 1000000.ToWords().ShouldBeSpelledAs("One million"); 1048576.ToWords().ShouldBeSpelledAs("One million Forty-Eight thousand Five hundred and Seventy-Six"); } public static void Main(string[] args) { TestUnits(); TestTens_10_19(); TestTens_20_99(); TestHundred_100_999(); TestThousands(); TestMillions(); } } }
run
|
edit
|
history
|
help
0
Concatenate string with null.
UnityCubo
Recursion
as
1
Static Class
decimal to binary
Intuit // C# // listing_4.8 (while)
DateTime Leap Day
tablecsv