|
using System; |
|
using System.Linq; |
|
using System.Collections.Generic; |
|
using System.ComponentModel; |
|
|
|
using BenchmarkDotNet.Attributes; |
|
using BenchmarkDotNet.Running; |
|
using BenchmarkDotNet.Configs; |
|
using BenchmarkDotNet.Jobs; |
|
|
|
namespace ConsoleApp0 |
|
{ |
|
[MemoryDiagnoser] |
|
public class Program |
|
{ |
|
private readonly string[] PrecomputedDisplayNameAttributeValues = PrecomputeDisplayNameAttributeValues(); |
|
|
|
private readonly Dictionary<int, string> dict = Enumerable.Range(1, 300).ToDictionary(i => i, i => i.ToString("x")); |
|
private readonly int[] intsToCheck = Enumerable.Range(1, 300).ToArray(); |
|
private readonly ArbitraryValues[] valsToCheck = Enumerable.Range(1, 300).Select(i => Enum.Parse<ArbitraryValues>(HumanFriendlyInteger.IntegerToWritten(i))).ToArray(); |
|
|
|
static int Main() |
|
{ |
|
// test that all results are identical. |
|
var program = new Program(); |
|
var lst1 = program.TestUsingPrecomputedDisplayNameAttributeValues(); |
|
var lst2 = program.TestUsingDict(); |
|
var lst3 = program.TestUsingSwitch(); |
|
var lst4 = program.TestUsingDisplayNameAttributeOnTheFly(); |
|
if (!(lst1.SequenceEqual(lst2) && |
|
lst1.SequenceEqual(lst3) && |
|
lst1.SequenceEqual(lst4))) |
|
{ |
|
Console.Error.WriteLine("Broken."); |
|
return 1; |
|
} |
|
|
|
BenchmarkRunner.Run<Program>( |
|
ManualConfig.Create( |
|
DefaultConfig.Instance.With( |
|
Job.Default.WithGcServer(true)))); |
|
return 0; |
|
} |
|
|
|
[Benchmark(Baseline = true)] |
|
public string[] TestUsingPrecomputedDisplayNameAttributeValues() |
|
{ |
|
return Array.ConvertAll(valsToCheck, val => PrecomputedDisplayNameAttributeValues[(int)val]); |
|
} |
|
|
|
[Benchmark] |
|
public string[] TestUsingDict() |
|
{ |
|
return Array.ConvertAll(intsToCheck, FindIntInDict); |
|
} |
|
|
|
[Benchmark] |
|
public string[] TestUsingSwitch() |
|
{ |
|
return Array.ConvertAll(intsToCheck, FindViaSwitch); |
|
} |
|
|
|
[Benchmark] |
|
public string[] TestUsingDisplayNameAttributeOnTheFly() |
|
{ |
|
return Array.ConvertAll(valsToCheck, FindViaDisplayNameAttribute); |
|
} |
|
|
|
private string FindIntInDict(int i) |
|
{ |
|
return dict.TryGetValue(i, out string val) |
|
? val |
|
: throw new ArgumentException($"No such key: {i}"); |
|
} |
|
|
|
private static string FindViaSwitch(int i) |
|
{ |
|
switch (i) |
|
{ |
|
case 1: |
|
return "1"; |
|
case 2: |
|
return "2"; |
|
case 3: |
|
return "3"; |
|
case 4: |
|
return "4"; |
|
case 5: |
|
return "5"; |
|
case 6: |
|
return "6"; |
|
case 7: |
|
return "7"; |
|
case 8: |
|
return "8"; |
|
case 9: |
|
return "9"; |
|
case 10: |
|
return "a"; |
|
case 11: |
|
return "b"; |
|
case 12: |
|
return "c"; |
|
case 13: |
|
return "d"; |
|
case 14: |
|
return "e"; |
|
case 15: |
|
return "f"; |
|
case 16: |
|
return "10"; |
|
case 17: |
|
return "11"; |
|
case 18: |
|
return "12"; |
|
case 19: |
|
return "13"; |
|
case 20: |
|
return "14"; |
|
case 21: |
|
return "15"; |
|
case 22: |
|
return "16"; |
|
case 23: |
|
return "17"; |
|
case 24: |
|
return "18"; |
|
case 25: |
|
return "19"; |
|
case 26: |
|
return "1a"; |
|
case 27: |
|
return "1b"; |
|
case 28: |
|
return "1c"; |
|
case 29: |
|
return "1d"; |
|
case 30: |
|
return "1e"; |
|
case 31: |
|
return "1f"; |
|
case 32: |
|
return "20"; |
|
case 33: |
|
return "21"; |
|
case 34: |
|
return "22"; |
|
case 35: |
|
return "23"; |
|
case 36: |
|
return "24"; |
|
case 37: |
|
return "25"; |
|
case 38: |
|
return "26"; |
|
case 39: |
|
return "27"; |
|
case 40: |
|
return "28"; |
|
case 41: |
|
return "29"; |
|
case 42: |
|
return "2a"; |
|
case 43: |
|
return "2b"; |
|
case 44: |
|
return "2c"; |
|
case 45: |
|
return "2d"; |
|
case 46: |
|
return "2e"; |
|
case 47: |
|
return "2f"; |
|
case 48: |
|
return "30"; |
|
case 49: |
|
return "31"; |
|
case 50: |
|
return "32"; |
|
case 51: |
|
return "33"; |
|
case 52: |
|
return "34"; |
|
case 53: |
|
return "35"; |
|
case 54: |
|
return "36"; |
|
case 55: |
|
return "37"; |
|
case 56: |
|
return "38"; |
|
case 57: |
|
return "39"; |
|
case 58: |
|
return "3a"; |
|
case 59: |
|
return "3b"; |
|
case 60: |
|
return "3c"; |
|
case 61: |
|
return "3d"; |
|
case 62: |
|
return "3e"; |
|
case 63: |
|
return "3f"; |
|
case 64: |
|
return "40"; |
|
case 65: |
|
return "41"; |
|
case 66: |
|
return "42"; |
|
case 67: |
|
return "43"; |
|
case 68: |
|
return "44"; |
|
case 69: |
|
return "45"; |
|
case 70: |
|
return "46"; |
|
case 71: |
|
return "47"; |
|
case 72: |
|
return "48"; |
|
case 73: |
|
return "49"; |
|
case 74: |
|
return "4a"; |
|
case 75: |
|
return "4b"; |
|
case 76: |
|
return "4c"; |
|
case 77: |
|
return "4d"; |
|
case 78: |
|
return "4e"; |
|
case 79: |
|
return "4f"; |
|
case 80: |
|
return "50"; |
|
case 81: |
|
return "51"; |
|
case 82: |
|
return "52"; |
|
case 83: |
|
return "53"; |
|
case 84: |
|
return "54"; |
|
case 85: |
|
return "55"; |
|
case 86: |
|
return "56"; |
|
case 87: |
|
return "57"; |
|
case 88: |
|
return "58"; |
|
case 89: |
|
return "59"; |
|
case 90: |
|
return "5a"; |
|
case 91: |
|
return "5b"; |
|
case 92: |
|
return "5c"; |
|
case 93: |
|
return "5d"; |
|
case 94: |
|
return "5e"; |
|
case 95: |
|
return "5f"; |
|
case 96: |
|
return "60"; |
|
case 97: |
|
return "61"; |
|
case 98: |
|
return "62"; |
|
case 99: |
|
return "63"; |
|
case 100: |
|
return "64"; |
|
case 101: |
|
return "65"; |
|
case 102: |
|
return "66"; |
|
case 103: |
|
return "67"; |
|
case 104: |
|
return "68"; |
|
case 105: |
|
return "69"; |
|
case 106: |
|
return "6a"; |
|
case 107: |
|
return "6b"; |
|
case 108: |
|
return "6c"; |
|
case 109: |
|
return "6d"; |
|
case 110: |
|
return "6e"; |
|
case 111: |
|
return "6f"; |
|
case 112: |
|
return "70"; |
|
case 113: |
|
return "71"; |
|
case 114: |
|
return "72"; |
|
case 115: |
|
return "73"; |
|
case 116: |
|
return "74"; |
|
case 117: |
|
return "75"; |
|
case 118: |
|
return "76"; |
|
case 119: |
|
return "77"; |
|
case 120: |
|
return "78"; |
|
case 121: |
|
return "79"; |
|
case 122: |
|
return "7a"; |
|
case 123: |
|
return "7b"; |
|
case 124: |
|
return "7c"; |
|
case 125: |
|
return "7d"; |
|
case 126: |
|
return "7e"; |
|
case 127: |
|
return "7f"; |
|
case 128: |
|
return "80"; |
|
case 129: |
|
return "81"; |
|
case 130: |
|
return "82"; |
|
case 131: |
|
return "83"; |
|
case 132: |
|
return "84"; |
|
case 133: |
|
return "85"; |
|
case 134: |
|
return "86"; |
|
case 135: |
|
return "87"; |
|
case 136: |
|
return "88"; |
|
case 137: |
|
return "89"; |
|
case 138: |
|
return "8a"; |
|
case 139: |
|
return "8b"; |
|
case 140: |
|
return "8c"; |
|
case 141: |
|
return "8d"; |
|
case 142: |
|
return "8e"; |
|
case 143: |
|
return "8f"; |
|
case 144: |
|
return "90"; |
|
case 145: |
|
return "91"; |
|
case 146: |
|
return "92"; |
|
case 147: |
|
return "93"; |
|
case 148: |
|
return "94"; |
|
case 149: |
|
return "95"; |
|
case 150: |
|
return "96"; |
|
case 151: |
|
return "97"; |
|
case 152: |
|
return "98"; |
|
case 153: |
|
return "99"; |
|
case 154: |
|
return "9a"; |
|
case 155: |
|
return "9b"; |
|
case 156: |
|
return "9c"; |
|
case 157: |
|
return "9d"; |
|
case 158: |
|
return "9e"; |
|
case 159: |
|
return "9f"; |
|
case 160: |
|
return "a0"; |
|
case 161: |
|
return "a1"; |
|
case 162: |
|
return "a2"; |
|
case 163: |
|
return "a3"; |
|
case 164: |
|
return "a4"; |
|
case 165: |
|
return "a5"; |
|
case 166: |
|
return "a6"; |
|
case 167: |
|
return "a7"; |
|
case 168: |
|
return "a8"; |
|
case 169: |
|
return "a9"; |
|
case 170: |
|
return "aa"; |
|
case 171: |
|
return "ab"; |
|
case 172: |
|
return "ac"; |
|
case 173: |
|
return "ad"; |
|
case 174: |
|
return "ae"; |
|
case 175: |
|
return "af"; |
|
case 176: |
|
return "b0"; |
|
case 177: |
|
return "b1"; |
|
case 178: |
|
return "b2"; |
|
case 179: |
|
return "b3"; |
|
case 180: |
|
return "b4"; |
|
case 181: |
|
return "b5"; |
|
case 182: |
|
return "b6"; |
|
case 183: |
|
return "b7"; |
|
case 184: |
|
return "b8"; |
|
case 185: |
|
return "b9"; |
|
case 186: |
|
return "ba"; |
|
case 187: |
|
return "bb"; |
|
case 188: |
|
return "bc"; |
|
case 189: |
|
return "bd"; |
|
case 190: |
|
return "be"; |
|
case 191: |
|
return "bf"; |
|
case 192: |
|
return "c0"; |
|
case 193: |
|
return "c1"; |
|
case 194: |
|
return "c2"; |
|
case 195: |
|
return "c3"; |
|
case 196: |
|
return "c4"; |
|
case 197: |
|
return "c5"; |
|
case 198: |
|
return "c6"; |
|
case 199: |
|
return "c7"; |
|
case 200: |
|
return "c8"; |
|
case 201: |
|
return "c9"; |
|
case 202: |
|
return "ca"; |
|
case 203: |
|
return "cb"; |
|
case 204: |
|
return "cc"; |
|
case 205: |
|
return "cd"; |
|
case 206: |
|
return "ce"; |
|
case 207: |
|
return "cf"; |
|
case 208: |
|
return "d0"; |
|
case 209: |
|
return "d1"; |
|
case 210: |
|
return "d2"; |
|
case 211: |
|
return "d3"; |
|
case 212: |
|
return "d4"; |
|
case 213: |
|
return "d5"; |
|
case 214: |
|
return "d6"; |
|
case 215: |
|
return "d7"; |
|
case 216: |
|
return "d8"; |
|
case 217: |
|
return "d9"; |
|
case 218: |
|
return "da"; |
|
case 219: |
|
return "db"; |
|
case 220: |
|
return "dc"; |
|
case 221: |
|
return "dd"; |
|
case 222: |
|
return "de"; |
|
case 223: |
|
return "df"; |
|
case 224: |
|
return "e0"; |
|
case 225: |
|
return "e1"; |
|
case 226: |
|
return "e2"; |
|
case 227: |
|
return "e3"; |
|
case 228: |
|
return "e4"; |
|
case 229: |
|
return "e5"; |
|
case 230: |
|
return "e6"; |
|
case 231: |
|
return "e7"; |
|
case 232: |
|
return "e8"; |
|
case 233: |
|
return "e9"; |
|
case 234: |
|
return "ea"; |
|
case 235: |
|
return "eb"; |
|
case 236: |
|
return "ec"; |
|
case 237: |
|
return "ed"; |
|
case 238: |
|
return "ee"; |
|
case 239: |
|
return "ef"; |
|
case 240: |
|
return "f0"; |
|
case 241: |
|
return "f1"; |
|
case 242: |
|
return "f2"; |
|
case 243: |
|
return "f3"; |
|
case 244: |
|
return "f4"; |
|
case 245: |
|
return "f5"; |
|
case 246: |
|
return "f6"; |
|
case 247: |
|
return "f7"; |
|
case 248: |
|
return "f8"; |
|
case 249: |
|
return "f9"; |
|
case 250: |
|
return "fa"; |
|
case 251: |
|
return "fb"; |
|
case 252: |
|
return "fc"; |
|
case 253: |
|
return "fd"; |
|
case 254: |
|
return "fe"; |
|
case 255: |
|
return "ff"; |
|
case 256: |
|
return "100"; |
|
case 257: |
|
return "101"; |
|
case 258: |
|
return "102"; |
|
case 259: |
|
return "103"; |
|
case 260: |
|
return "104"; |
|
case 261: |
|
return "105"; |
|
case 262: |
|
return "106"; |
|
case 263: |
|
return "107"; |
|
case 264: |
|
return "108"; |
|
case 265: |
|
return "109"; |
|
case 266: |
|
return "10a"; |
|
case 267: |
|
return "10b"; |
|
case 268: |
|
return "10c"; |
|
case 269: |
|
return "10d"; |
|
case 270: |
|
return "10e"; |
|
case 271: |
|
return "10f"; |
|
case 272: |
|
return "110"; |
|
case 273: |
|
return "111"; |
|
case 274: |
|
return "112"; |
|
case 275: |
|
return "113"; |
|
case 276: |
|
return "114"; |
|
case 277: |
|
return "115"; |
|
case 278: |
|
return "116"; |
|
case 279: |
|
return "117"; |
|
case 280: |
|
return "118"; |
|
case 281: |
|
return "119"; |
|
case 282: |
|
return "11a"; |
|
case 283: |
|
return "11b"; |
|
case 284: |
|
return "11c"; |
|
case 285: |
|
return "11d"; |
|
case 286: |
|
return "11e"; |
|
case 287: |
|
return "11f"; |
|
case 288: |
|
return "120"; |
|
case 289: |
|
return "121"; |
|
case 290: |
|
return "122"; |
|
case 291: |
|
return "123"; |
|
case 292: |
|
return "124"; |
|
case 293: |
|
return "125"; |
|
case 294: |
|
return "126"; |
|
case 295: |
|
return "127"; |
|
case 296: |
|
return "128"; |
|
case 297: |
|
return "129"; |
|
case 298: |
|
return "12a"; |
|
case 299: |
|
return "12b"; |
|
case 300: |
|
return "12c"; |
|
default: |
|
throw new ArgumentException($"No value defined for {i}"); |
|
} |
|
} |
|
|
|
private static string[] PrecomputeDisplayNameAttributeValues() |
|
{ |
|
var definedValues = (ArbitraryValues[])Enum.GetValues(typeof(ArbitraryValues)); |
|
int max = definedValues.Max(val => (int)val); |
|
string[] result = new string[max + 1]; |
|
foreach (var val in definedValues) |
|
{ |
|
result[(int)val] = FindViaDisplayNameAttribute(val); |
|
} |
|
|
|
return result; |
|
} |
|
|
|
private static string FindViaDisplayNameAttribute(ArbitraryValues i) |
|
{ |
|
var type = typeof(ArbitraryValues); |
|
var memInfo = type.GetMember(i.ToString()); |
|
if (memInfo.Length == 0) |
|
{ |
|
throw new ArgumentException($"No value defined for {i}"); |
|
} |
|
var attributes = memInfo[0].GetCustomAttributes(typeof(DisplayNameAttribute), false); |
|
if (attributes.Length == 0) |
|
{ |
|
throw new ArgumentException($"No value defined for {i}"); |
|
} |
|
return ((DisplayNameAttribute)attributes[0]).DisplayName; |
|
} |
|
} |
|
|
|
public enum ArbitraryValues |
|
{ |
|
[DisplayName("1")] |
|
One, |
|
[DisplayName("2")] |
|
Two, |
|
[DisplayName("3")] |
|
Three, |
|
[DisplayName("4")] |
|
Four, |
|
[DisplayName("5")] |
|
Five, |
|
[DisplayName("6")] |
|
Six, |
|
[DisplayName("7")] |
|
Seven, |
|
[DisplayName("8")] |
|
Eight, |
|
[DisplayName("9")] |
|
Nine, |
|
[DisplayName("a")] |
|
Ten, |
|
[DisplayName("b")] |
|
Eleven, |
|
[DisplayName("c")] |
|
Twelve, |
|
[DisplayName("d")] |
|
Thirteen, |
|
[DisplayName("e")] |
|
Fourteen, |
|
[DisplayName("f")] |
|
Fifteen, |
|
[DisplayName("10")] |
|
Sixteen, |
|
[DisplayName("11")] |
|
Seventeen, |
|
[DisplayName("12")] |
|
Eighteen, |
|
[DisplayName("13")] |
|
Nineteen, |
|
[DisplayName("14")] |
|
Twenty, |
|
[DisplayName("15")] |
|
TwentyOne, |
|
[DisplayName("16")] |
|
TwentyTwo, |
|
[DisplayName("17")] |
|
TwentyThree, |
|
[DisplayName("18")] |
|
TwentyFour, |
|
[DisplayName("19")] |
|
TwentyFive, |
|
[DisplayName("1a")] |
|
TwentySix, |
|
[DisplayName("1b")] |
|
TwentySeven, |
|
[DisplayName("1c")] |
|
TwentyEight, |
|
[DisplayName("1d")] |
|
TwentyNine, |
|
[DisplayName("1e")] |
|
Thirty, |
|
[DisplayName("1f")] |
|
ThirtyOne, |
|
[DisplayName("20")] |
|
ThirtyTwo, |
|
[DisplayName("21")] |
|
ThirtyThree, |
|
[DisplayName("22")] |
|
ThirtyFour, |
|
[DisplayName("23")] |
|
ThirtyFive, |
|
[DisplayName("24")] |
|
ThirtySix, |
|
[DisplayName("25")] |
|
ThirtySeven, |
|
[DisplayName("26")] |
|
ThirtyEight, |
|
[DisplayName("27")] |
|
ThirtyNine, |
|
[DisplayName("28")] |
|
Forty, |
|
[DisplayName("29")] |
|
FortyOne, |
|
[DisplayName("2a")] |
|
FortyTwo, |
|
[DisplayName("2b")] |
|
FortyThree, |
|
[DisplayName("2c")] |
|
FortyFour, |
|
[DisplayName("2d")] |
|
FortyFive, |
|
[DisplayName("2e")] |
|
FortySix, |
|
[DisplayName("2f")] |
|
FortySeven, |
|
[DisplayName("30")] |
|
FortyEight, |
|
[DisplayName("31")] |
|
FortyNine, |
|
[DisplayName("32")] |
|
Fifty, |
|
[DisplayName("33")] |
|
FiftyOne, |
|
[DisplayName("34")] |
|
FiftyTwo, |
|
[DisplayName("35")] |
|
FiftyThree, |
|
[DisplayName("36")] |
|
FiftyFour, |
|
[DisplayName("37")] |
|
FiftyFive, |
|
[DisplayName("38")] |
|
FiftySix, |
|
[DisplayName("39")] |
|
FiftySeven, |
|
[DisplayName("3a")] |
|
FiftyEight, |
|
[DisplayName("3b")] |
|
FiftyNine, |
|
[DisplayName("3c")] |
|
Sixty, |
|
[DisplayName("3d")] |
|
SixtyOne, |
|
[DisplayName("3e")] |
|
SixtyTwo, |
|
[DisplayName("3f")] |
|
SixtyThree, |
|
[DisplayName("40")] |
|
SixtyFour, |
|
[DisplayName("41")] |
|
SixtyFive, |
|
[DisplayName("42")] |
|
SixtySix, |
|
[DisplayName("43")] |
|
SixtySeven, |
|
[DisplayName("44")] |
|
SixtyEight, |
|
[DisplayName("45")] |
|
SixtyNine, |
|
[DisplayName("46")] |
|
Seventy, |
|
[DisplayName("47")] |
|
SeventyOne, |
|
[DisplayName("48")] |
|
SeventyTwo, |
|
[DisplayName("49")] |
|
SeventyThree, |
|
[DisplayName("4a")] |
|
SeventyFour, |
|
[DisplayName("4b")] |
|
SeventyFive, |
|
[DisplayName("4c")] |
|
SeventySix, |
|
[DisplayName("4d")] |
|
SeventySeven, |
|
[DisplayName("4e")] |
|
SeventyEight, |
|
[DisplayName("4f")] |
|
SeventyNine, |
|
[DisplayName("50")] |
|
Eighty, |
|
[DisplayName("51")] |
|
EightyOne, |
|
[DisplayName("52")] |
|
EightyTwo, |
|
[DisplayName("53")] |
|
EightyThree, |
|
[DisplayName("54")] |
|
EightyFour, |
|
[DisplayName("55")] |
|
EightyFive, |
|
[DisplayName("56")] |
|
EightySix, |
|
[DisplayName("57")] |
|
EightySeven, |
|
[DisplayName("58")] |
|
EightyEight, |
|
[DisplayName("59")] |
|
EightyNine, |
|
[DisplayName("5a")] |
|
Ninety, |
|
[DisplayName("5b")] |
|
NinetyOne, |
|
[DisplayName("5c")] |
|
NinetyTwo, |
|
[DisplayName("5d")] |
|
NinetyThree, |
|
[DisplayName("5e")] |
|
NinetyFour, |
|
[DisplayName("5f")] |
|
NinetyFive, |
|
[DisplayName("60")] |
|
NinetySix, |
|
[DisplayName("61")] |
|
NinetySeven, |
|
[DisplayName("62")] |
|
NinetyEight, |
|
[DisplayName("63")] |
|
NinetyNine, |
|
[DisplayName("64")] |
|
OneHundred, |
|
[DisplayName("65")] |
|
OneHundredOne, |
|
[DisplayName("66")] |
|
OneHundredTwo, |
|
[DisplayName("67")] |
|
OneHundredThree, |
|
[DisplayName("68")] |
|
OneHundredFour, |
|
[DisplayName("69")] |
|
OneHundredFive, |
|
[DisplayName("6a")] |
|
OneHundredSix, |
|
[DisplayName("6b")] |
|
OneHundredSeven, |
|
[DisplayName("6c")] |
|
OneHundredEight, |
|
[DisplayName("6d")] |
|
OneHundredNine, |
|
[DisplayName("6e")] |
|
OneHundredTen, |
|
[DisplayName("6f")] |
|
OneHundredEleven, |
|
[DisplayName("70")] |
|
OneHundredTwelve, |
|
[DisplayName("71")] |
|
OneHundredThirteen, |
|
[DisplayName("72")] |
|
OneHundredFourteen, |
|
[DisplayName("73")] |
|
OneHundredFifteen, |
|
[DisplayName("74")] |
|
OneHundredSixteen, |
|
[DisplayName("75")] |
|
OneHundredSeventeen, |
|
[DisplayName("76")] |
|
OneHundredEighteen, |
|
[DisplayName("77")] |
|
OneHundredNineteen, |
|
[DisplayName("78")] |
|
OneHundredTwenty, |
|
[DisplayName("79")] |
|
OneHundredTwentyOne, |
|
[DisplayName("7a")] |
|
OneHundredTwentyTwo, |
|
[DisplayName("7b")] |
|
OneHundredTwentyThree, |
|
[DisplayName("7c")] |
|
OneHundredTwentyFour, |
|
[DisplayName("7d")] |
|
OneHundredTwentyFive, |
|
[DisplayName("7e")] |
|
OneHundredTwentySix, |
|
[DisplayName("7f")] |
|
OneHundredTwentySeven, |
|
[DisplayName("80")] |
|
OneHundredTwentyEight, |
|
[DisplayName("81")] |
|
OneHundredTwentyNine, |
|
[DisplayName("82")] |
|
OneHundredThirty, |
|
[DisplayName("83")] |
|
OneHundredThirtyOne, |
|
[DisplayName("84")] |
|
OneHundredThirtyTwo, |
|
[DisplayName("85")] |
|
OneHundredThirtyThree, |
|
[DisplayName("86")] |
|
OneHundredThirtyFour, |
|
[DisplayName("87")] |
|
OneHundredThirtyFive, |
|
[DisplayName("88")] |
|
OneHundredThirtySix, |
|
[DisplayName("89")] |
|
OneHundredThirtySeven, |
|
[DisplayName("8a")] |
|
OneHundredThirtyEight, |
|
[DisplayName("8b")] |
|
OneHundredThirtyNine, |
|
[DisplayName("8c")] |
|
OneHundredForty, |
|
[DisplayName("8d")] |
|
OneHundredFortyOne, |
|
[DisplayName("8e")] |
|
OneHundredFortyTwo, |
|
[DisplayName("8f")] |
|
OneHundredFortyThree, |
|
[DisplayName("90")] |
|
OneHundredFortyFour, |
|
[DisplayName("91")] |
|
OneHundredFortyFive, |
|
[DisplayName("92")] |
|
OneHundredFortySix, |
|
[DisplayName("93")] |
|
OneHundredFortySeven, |
|
[DisplayName("94")] |
|
OneHundredFortyEight, |
|
[DisplayName("95")] |
|
OneHundredFortyNine, |
|
[DisplayName("96")] |
|
OneHundredFifty, |
|
[DisplayName("97")] |
|
OneHundredFiftyOne, |
|
[DisplayName("98")] |
|
OneHundredFiftyTwo, |
|
[DisplayName("99")] |
|
OneHundredFiftyThree, |
|
[DisplayName("9a")] |
|
OneHundredFiftyFour, |
|
[DisplayName("9b")] |
|
OneHundredFiftyFive, |
|
[DisplayName("9c")] |
|
OneHundredFiftySix, |
|
[DisplayName("9d")] |
|
OneHundredFiftySeven, |
|
[DisplayName("9e")] |
|
OneHundredFiftyEight, |
|
[DisplayName("9f")] |
|
OneHundredFiftyNine, |
|
[DisplayName("a0")] |
|
OneHundredSixty, |
|
[DisplayName("a1")] |
|
OneHundredSixtyOne, |
|
[DisplayName("a2")] |
|
OneHundredSixtyTwo, |
|
[DisplayName("a3")] |
|
OneHundredSixtyThree, |
|
[DisplayName("a4")] |
|
OneHundredSixtyFour, |
|
[DisplayName("a5")] |
|
OneHundredSixtyFive, |
|
[DisplayName("a6")] |
|
OneHundredSixtySix, |
|
[DisplayName("a7")] |
|
OneHundredSixtySeven, |
|
[DisplayName("a8")] |
|
OneHundredSixtyEight, |
|
[DisplayName("a9")] |
|
OneHundredSixtyNine, |
|
[DisplayName("aa")] |
|
OneHundredSeventy, |
|
[DisplayName("ab")] |
|
OneHundredSeventyOne, |
|
[DisplayName("ac")] |
|
OneHundredSeventyTwo, |
|
[DisplayName("ad")] |
|
OneHundredSeventyThree, |
|
[DisplayName("ae")] |
|
OneHundredSeventyFour, |
|
[DisplayName("af")] |
|
OneHundredSeventyFive, |
|
[DisplayName("b0")] |
|
OneHundredSeventySix, |
|
[DisplayName("b1")] |
|
OneHundredSeventySeven, |
|
[DisplayName("b2")] |
|
OneHundredSeventyEight, |
|
[DisplayName("b3")] |
|
OneHundredSeventyNine, |
|
[DisplayName("b4")] |
|
OneHundredEighty, |
|
[DisplayName("b5")] |
|
OneHundredEightyOne, |
|
[DisplayName("b6")] |
|
OneHundredEightyTwo, |
|
[DisplayName("b7")] |
|
OneHundredEightyThree, |
|
[DisplayName("b8")] |
|
OneHundredEightyFour, |
|
[DisplayName("b9")] |
|
OneHundredEightyFive, |
|
[DisplayName("ba")] |
|
OneHundredEightySix, |
|
[DisplayName("bb")] |
|
OneHundredEightySeven, |
|
[DisplayName("bc")] |
|
OneHundredEightyEight, |
|
[DisplayName("bd")] |
|
OneHundredEightyNine, |
|
[DisplayName("be")] |
|
OneHundredNinety, |
|
[DisplayName("bf")] |
|
OneHundredNinetyOne, |
|
[DisplayName("c0")] |
|
OneHundredNinetyTwo, |
|
[DisplayName("c1")] |
|
OneHundredNinetyThree, |
|
[DisplayName("c2")] |
|
OneHundredNinetyFour, |
|
[DisplayName("c3")] |
|
OneHundredNinetyFive, |
|
[DisplayName("c4")] |
|
OneHundredNinetySix, |
|
[DisplayName("c5")] |
|
OneHundredNinetySeven, |
|
[DisplayName("c6")] |
|
OneHundredNinetyEight, |
|
[DisplayName("c7")] |
|
OneHundredNinetyNine, |
|
[DisplayName("c8")] |
|
TwoHundred, |
|
[DisplayName("c9")] |
|
TwoHundredOne, |
|
[DisplayName("ca")] |
|
TwoHundredTwo, |
|
[DisplayName("cb")] |
|
TwoHundredThree, |
|
[DisplayName("cc")] |
|
TwoHundredFour, |
|
[DisplayName("cd")] |
|
TwoHundredFive, |
|
[DisplayName("ce")] |
|
TwoHundredSix, |
|
[DisplayName("cf")] |
|
TwoHundredSeven, |
|
[DisplayName("d0")] |
|
TwoHundredEight, |
|
[DisplayName("d1")] |
|
TwoHundredNine, |
|
[DisplayName("d2")] |
|
TwoHundredTen, |
|
[DisplayName("d3")] |
|
TwoHundredEleven, |
|
[DisplayName("d4")] |
|
TwoHundredTwelve, |
|
[DisplayName("d5")] |
|
TwoHundredThirteen, |
|
[DisplayName("d6")] |
|
TwoHundredFourteen, |
|
[DisplayName("d7")] |
|
TwoHundredFifteen, |
|
[DisplayName("d8")] |
|
TwoHundredSixteen, |
|
[DisplayName("d9")] |
|
TwoHundredSeventeen, |
|
[DisplayName("da")] |
|
TwoHundredEighteen, |
|
[DisplayName("db")] |
|
TwoHundredNineteen, |
|
[DisplayName("dc")] |
|
TwoHundredTwenty, |
|
[DisplayName("dd")] |
|
TwoHundredTwentyOne, |
|
[DisplayName("de")] |
|
TwoHundredTwentyTwo, |
|
[DisplayName("df")] |
|
TwoHundredTwentyThree, |
|
[DisplayName("e0")] |
|
TwoHundredTwentyFour, |
|
[DisplayName("e1")] |
|
TwoHundredTwentyFive, |
|
[DisplayName("e2")] |
|
TwoHundredTwentySix, |
|
[DisplayName("e3")] |
|
TwoHundredTwentySeven, |
|
[DisplayName("e4")] |
|
TwoHundredTwentyEight, |
|
[DisplayName("e5")] |
|
TwoHundredTwentyNine, |
|
[DisplayName("e6")] |
|
TwoHundredThirty, |
|
[DisplayName("e7")] |
|
TwoHundredThirtyOne, |
|
[DisplayName("e8")] |
|
TwoHundredThirtyTwo, |
|
[DisplayName("e9")] |
|
TwoHundredThirtyThree, |
|
[DisplayName("ea")] |
|
TwoHundredThirtyFour, |
|
[DisplayName("eb")] |
|
TwoHundredThirtyFive, |
|
[DisplayName("ec")] |
|
TwoHundredThirtySix, |
|
[DisplayName("ed")] |
|
TwoHundredThirtySeven, |
|
[DisplayName("ee")] |
|
TwoHundredThirtyEight, |
|
[DisplayName("ef")] |
|
TwoHundredThirtyNine, |
|
[DisplayName("f0")] |
|
TwoHundredForty, |
|
[DisplayName("f1")] |
|
TwoHundredFortyOne, |
|
[DisplayName("f2")] |
|
TwoHundredFortyTwo, |
|
[DisplayName("f3")] |
|
TwoHundredFortyThree, |
|
[DisplayName("f4")] |
|
TwoHundredFortyFour, |
|
[DisplayName("f5")] |
|
TwoHundredFortyFive, |
|
[DisplayName("f6")] |
|
TwoHundredFortySix, |
|
[DisplayName("f7")] |
|
TwoHundredFortySeven, |
|
[DisplayName("f8")] |
|
TwoHundredFortyEight, |
|
[DisplayName("f9")] |
|
TwoHundredFortyNine, |
|
[DisplayName("fa")] |
|
TwoHundredFifty, |
|
[DisplayName("fb")] |
|
TwoHundredFiftyOne, |
|
[DisplayName("fc")] |
|
TwoHundredFiftyTwo, |
|
[DisplayName("fd")] |
|
TwoHundredFiftyThree, |
|
[DisplayName("fe")] |
|
TwoHundredFiftyFour, |
|
[DisplayName("ff")] |
|
TwoHundredFiftyFive, |
|
[DisplayName("100")] |
|
TwoHundredFiftySix, |
|
[DisplayName("101")] |
|
TwoHundredFiftySeven, |
|
[DisplayName("102")] |
|
TwoHundredFiftyEight, |
|
[DisplayName("103")] |
|
TwoHundredFiftyNine, |
|
[DisplayName("104")] |
|
TwoHundredSixty, |
|
[DisplayName("105")] |
|
TwoHundredSixtyOne, |
|
[DisplayName("106")] |
|
TwoHundredSixtyTwo, |
|
[DisplayName("107")] |
|
TwoHundredSixtyThree, |
|
[DisplayName("108")] |
|
TwoHundredSixtyFour, |
|
[DisplayName("109")] |
|
TwoHundredSixtyFive, |
|
[DisplayName("10a")] |
|
TwoHundredSixtySix, |
|
[DisplayName("10b")] |
|
TwoHundredSixtySeven, |
|
[DisplayName("10c")] |
|
TwoHundredSixtyEight, |
|
[DisplayName("10d")] |
|
TwoHundredSixtyNine, |
|
[DisplayName("10e")] |
|
TwoHundredSeventy, |
|
[DisplayName("10f")] |
|
TwoHundredSeventyOne, |
|
[DisplayName("110")] |
|
TwoHundredSeventyTwo, |
|
[DisplayName("111")] |
|
TwoHundredSeventyThree, |
|
[DisplayName("112")] |
|
TwoHundredSeventyFour, |
|
[DisplayName("113")] |
|
TwoHundredSeventyFive, |
|
[DisplayName("114")] |
|
TwoHundredSeventySix, |
|
[DisplayName("115")] |
|
TwoHundredSeventySeven, |
|
[DisplayName("116")] |
|
TwoHundredSeventyEight, |
|
[DisplayName("117")] |
|
TwoHundredSeventyNine, |
|
[DisplayName("118")] |
|
TwoHundredEighty, |
|
[DisplayName("119")] |
|
TwoHundredEightyOne, |
|
[DisplayName("11a")] |
|
TwoHundredEightyTwo, |
|
[DisplayName("11b")] |
|
TwoHundredEightyThree, |
|
[DisplayName("11c")] |
|
TwoHundredEightyFour, |
|
[DisplayName("11d")] |
|
TwoHundredEightyFive, |
|
[DisplayName("11e")] |
|
TwoHundredEightySix, |
|
[DisplayName("11f")] |
|
TwoHundredEightySeven, |
|
[DisplayName("120")] |
|
TwoHundredEightyEight, |
|
[DisplayName("121")] |
|
TwoHundredEightyNine, |
|
[DisplayName("122")] |
|
TwoHundredNinety, |
|
[DisplayName("123")] |
|
TwoHundredNinetyOne, |
|
[DisplayName("124")] |
|
TwoHundredNinetyTwo, |
|
[DisplayName("125")] |
|
TwoHundredNinetyThree, |
|
[DisplayName("126")] |
|
TwoHundredNinetyFour, |
|
[DisplayName("127")] |
|
TwoHundredNinetyFive, |
|
[DisplayName("128")] |
|
TwoHundredNinetySix, |
|
[DisplayName("129")] |
|
TwoHundredNinetySeven, |
|
[DisplayName("12a")] |
|
TwoHundredNinetyEight, |
|
[DisplayName("12b")] |
|
TwoHundredNinetyNine, |
|
[DisplayName("12c")] |
|
ThreeHundred |
|
} |
|
|
|
public static class HumanFriendlyInteger |
|
{ |
|
static string[] ones = new string[] { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" }; |
|
static string[] teens = new string[] { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; |
|
static string[] tens = new string[] { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" }; |
|
static string[] thousandsGroups = { "", "Thousand", "Million", "Billion" }; |
|
|
|
private static string FriendlyInteger(int n, string leftDigits, int thousands) |
|
{ |
|
if (n == 0) |
|
{ |
|
return leftDigits; |
|
} |
|
|
|
string friendlyInt = leftDigits; |
|
|
|
if (friendlyInt.Length > 0) |
|
{ |
|
friendlyInt += ""; |
|
} |
|
|
|
if (n < 10) |
|
{ |
|
friendlyInt += ones[n]; |
|
} |
|
else if (n < 20) |
|
{ |
|
friendlyInt += teens[n - 10]; |
|
} |
|
else if (n < 100) |
|
{ |
|
friendlyInt += FriendlyInteger(n % 10, tens[n / 10 - 2], 0); |
|
} |
|
else if (n < 1000) |
|
{ |
|
friendlyInt += FriendlyInteger(n % 100, (ones[n / 100] + "Hundred"), 0); |
|
} |
|
else |
|
{ |
|
friendlyInt += FriendlyInteger(n % 1000, FriendlyInteger(n / 1000, "", thousands + 1), 0); |
|
if (n % 1000 == 0) |
|
{ |
|
return friendlyInt; |
|
} |
|
} |
|
|
|
return friendlyInt + thousandsGroups[thousands]; |
|
} |
|
|
|
public static string IntegerToWritten(int n) |
|
{ |
|
if (n == 0) |
|
{ |
|
return "Zero"; |
|
} |
|
else if (n < 0) |
|
{ |
|
return "Negative" + IntegerToWritten(-n); |
|
} |
|
|
|
return FriendlyInteger(n, "", 0); |
|
} |
|
} |
|
} |