/
NumberText.cs
116 lines (100 loc) · 3.63 KB
/
NumberText.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NumberText {
public static class NumberTextExtensionMethod {
public static string ToText(this int num) {
var numberText = new NumberText();
return numberText.ToText(num);
}
}
public class NumberText {
private Dictionary<int, string> textStrings = new Dictionary<int, string>();
private Dictionary<int, string> scales = new Dictionary<int, string>();
private StringBuilder builder;
public NumberText() {
Initialize();
}
public string ToText(int num) {
builder = new StringBuilder();
if (num == 0) {
builder.Append(textStrings[num]);
return builder.ToString();
}
num = scales.Aggregate(num, (current, scale) => Append(current, scale.Key));
AppendLessThanOneThousand(num);
return builder.ToString().Trim();
}
private int Append(int num, int scale) {
if (num > scale - 1) {
var baseScale = ((int)(num / scale));
AppendLessThanOneThousand(baseScale);
builder.AppendFormat("{0} ", scales[scale]);
num = num - (baseScale * scale);
}
return num;
}
private int AppendLessThanOneThousand(int num) {
num = AppendHundreds(num);
num = AppendTens(num);
AppendUnits(num);
return num;
}
private void AppendUnits(int num) {
if (num > 0) {
builder.AppendFormat("{0} ", textStrings[num]);
}
}
private int AppendTens(int num) {
if (num > 20) {
var tens = ((int) (num/10))*10;
builder.AppendFormat("{0} ", textStrings[tens]);
num = num - tens;
}
return num;
}
private int AppendHundreds(int num) {
if (num > 99) {
var hundreds = ((int) (num/100));
builder.AppendFormat("{0} hundred ", textStrings[hundreds]);
num = num - (hundreds*100);
}
return num;
}
private void Initialize() {
textStrings.Add(0, "zero");
textStrings.Add(1, "one");
textStrings.Add(2, "two");
textStrings.Add(3, "three");
textStrings.Add(4, "four");
textStrings.Add(5, "five");
textStrings.Add(6, "six");
textStrings.Add(7, "seven");
textStrings.Add(8, "eight");
textStrings.Add(9, "nine");
textStrings.Add(10, "ten");
textStrings.Add(11, "eleven");
textStrings.Add(12, "twelve");
textStrings.Add(13, "thirteen");
textStrings.Add(14, "fourteen");
textStrings.Add(15, "fifteen");
textStrings.Add(16, "sixteen");
textStrings.Add(17, "seventeen");
textStrings.Add(18, "eighteen");
textStrings.Add(19, "nineteen");
textStrings.Add(20, "twenty");
textStrings.Add(30, "thirty");
textStrings.Add(40, "forty");
textStrings.Add(50, "fifty");
textStrings.Add(60, "sixty");
textStrings.Add(70, "seventy");
textStrings.Add(80, "eighty");
textStrings.Add(90, "ninety");
textStrings.Add(100, "hundred");
scales.Add(1000000000, "billion");
scales.Add(1000000, "million");
scales.Add(1000, "thousand");
}
}
}