1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: namespace Rundiz\Number;
15:
16: 17: 18: 19: 20: 21: 22: 23:
24: class NumberEng
25: {
26:
27:
28: 29: 30: 31: 32: 33:
34: private function convertDigit($digit)
35: {
36: switch($digit)
37: {
38: case '0': return 'zero';
39: case '1': return 'one';
40: case '2': return 'two';
41: case '3': return 'three';
42: case '4': return 'four';
43: case '5': return 'five';
44: case '6': return 'six';
45: case '7': return 'seven';
46: case '8': return 'eight';
47: case '9': return 'nine';
48: }
49: }
50:
51:
52: 53: 54: 55: 56: 57:
58: private function convertGroup($index)
59: {
60: switch ($index) {
61: case 11: return ' decillion';
62: case 10: return ' nonillion';
63: case 9: return ' octillion';
64: case 8: return ' septillion';
65: case 7: return ' sextillion';
66: case 6: return ' quintrillion';
67: case 5: return ' quadrillion';
68: case 4: return ' trillion';
69: case 3: return ' billion';
70: case 2: return ' million';
71: case 1: return ' thousand';
72: case 0: return '';
73: }
74: }
75:
76: 77: 78: 79: 80: 81:
82: public function convertNumber($num)
83: {
84: $num = strval($num);
85:
86: if (strpos($num, '.') !== false) {
87: list($num, $dec) = explode('.', $num);
88: } else {
89: $dec = 0;
90: }
91:
92: $output = '';
93:
94: if ($num{0} == '-') {
95: $output = 'negative ';
96: $num = ltrim($num, '-');
97: } else if ($num{0} == '+') {
98: $output = 'positive ';
99: $num = ltrim($num, '+');
100: }
101:
102: if ($num{0} == '0') {
103: $output .= 'zero';
104: } else {
105: if (strlen($num) > 36) {
106: return 'Error!';
107: }
108:
109: $num = str_pad($num, 36, '0', STR_PAD_LEFT);
110: $group = rtrim(chunk_split($num, 3, ' '), ' ');
111: $groups = explode(' ', $group);
112:
113: $groups2 = array();
114: foreach ($groups as $g) {
115: $groups2[] = $this->convertThreeDigit($g{0}, $g{1}, $g{2});
116: }
117:
118: for ($z = 0; $z < count($groups2); $z++) {
119: if ($groups2[$z] != '') {
120: $output .= $groups2[$z] . $this->convertGroup(11 - $z) . ($z < 11 && !array_search('', array_slice($groups2, $z + 1, -1)) && $groups2[11] != '' && $groups[11]{0} == '0' ? ' and ' : ', ');
121: }
122: }
123:
124: $output = rtrim($output, ', ');
125: }
126:
127: if ($dec > 0) {
128: $output .= ' point';
129: for ($i = 0; $i < strlen($dec); $i++) {
130: $output .= ' ' . $this->convertDigit($dec{$i});
131: }
132: }
133:
134: return $output;
135: }
136:
137:
138: 139: 140: 141: 142: 143: 144: 145:
146: private function convertThreeDigit($dig1, $dig2, $dig3)
147: {
148: $output = '';
149:
150: if ($dig1 == '0' && $dig2 == '0' && $dig3 == '0') {
151: return '';
152: }
153:
154: if ($dig1 != '0') {
155: $output .= $this->convertDigit($dig1) . ' hundred';
156: if ($dig2 != '0' || $dig3 != '0') {
157: $output .= ' and ';
158: }
159: }
160:
161: if ($dig2 != '0') {
162: $output .= $this->convertTwoDigit($dig2, $dig3);
163: } else if ($dig3 != '0') {
164: $output .= $this->convertDigit($dig3);
165: }
166:
167: return $output;
168: }
169:
170:
171: 172: 173: 174: 175: 176: 177:
178: private function convertTwoDigit($dig1, $dig2)
179: {
180: if ($dig2 == '0') {
181: switch ($dig1) {
182: case '1': return 'ten';
183: case '2': return 'twenty';
184: case '3': return 'thirty';
185: case '4': return 'forty';
186: case '5': return 'fifty';
187: case '6': return 'sixty';
188: case '7': return 'seventy';
189: case '8': return 'eighty';
190: case '9': return 'ninety';
191: }
192: } else if ($dig1 == '1') {
193: switch ($dig2) {
194: case '1': return 'eleven';
195: case '2': return 'twelve';
196: case '3': return 'thirteen';
197: case '4': return 'fourteen';
198: case '5': return 'fifteen';
199: case '6': return 'sixteen';
200: case '7': return 'seventeen';
201: case '8': return 'eighteen';
202: case '9': return 'nineteen';
203: }
204: } else {
205: $temp = $this->convertDigit($dig2);
206: switch ($dig1) {
207: case '2': return "twenty-$temp";
208: case '3': return "thirty-$temp";
209: case '4': return "forty-$temp";
210: case '5': return "fifty-$temp";
211: case '6': return "sixty-$temp";
212: case '7': return "seventy-$temp";
213: case '8': return "eighty-$temp";
214: case '9': return "ninety-$temp";
215: }
216: }
217: }
218:
219: }
220: