Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Numbers_Words
4
 *
5
 * PHP version 4
6
 *
7
 * Copyright (c) 1997-2006 The PHP Group
8
 *
9
 * This source file is subject to version 3.0 of the PHP license,
10
 * that is bundled with this package in the file LICENSE, and is
11
 * available at through the world-wide-web at
12
 * http://www.php.net/license/3_0.txt.
13
 * If you did not receive a copy of the PHP license and are unable to
14
 * obtain it through the world-wide-web, please send a note to
15
 * license@php.net so we can mail you a copy immediately.
16
 *
17
 * @category Numbers
18
 * @package  Numbers_Words
19
 * @author   Piotr Klaban <makler@man.torun.pl>
20
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
21
 * @version  CVS: $Id: lang.en_100.php 269648 2008-11-25 02:45:52Z clockwerx $
22
 * @link     http://pear.php.net/package/Numbers_Words
23
 */
24
 
25
/**
26
 * Class for translating numbers into Donald Knuth system, in English language.
27
 *
28
 * @author Piotr Klaban
29
 * @package Numbers_Words
30
 */
31
 
32
/**
33
 * Include needed files
34
 */
35
require_once "Numbers/Words.php";
36
 
37
/**
38
 * Class for translating numbers into Donald Knuth system, in English language.
39
 *
40
 * @category Numbers
41
 * @package  Numbers_Words
42
 * @author   Piotr Klaban <makler@man.torun.pl>
43
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
44
 * @link     http://pear.php.net/package/Numbers_Words
45
 */
46
class Numbers_Words_en_100 extends Numbers_Words
47
{
48
 
49
    // {{{ properties
50
 
51
    /**
52
     * Locale name
53
     * @var string
54
     * @access public
55
     */
56
    var $locale = 'en_100';
57
 
58
    /**
59
     * Language name in English
60
     * @var string
61
     * @access public
62
     */
63
    var $lang = 'English (Donald Knuth system)';
64
 
65
    /**
66
     * Native language name
67
     * @var string
68
     * @access public
69
     */
70
    var $lang_native = 'English (Donald Knuth system)';
71
 
72
    /**
73
     * The word for the minus sign
74
     * @var string
75
     * @access private
76
     */
77
    var $_minus = 'minus'; // minus sign
78
 
79
    /**
80
     * The sufixes for exponents (singular and plural)
81
     * Names based on:
82
     * http://home.earthlink.net/~mrob/pub/math/largenum.html
83
     * Donald Knuth system (power of 2)
84
     * @var array
85
     * @access private
86
     */
87
    var $_exponent = array(
88
 
89
        2 => array('hundred'),
90
        4 => array('myriad'),
91
        8 => array('myllion'),
92
       16 => array('byllion'),
93
       32 => array('tryllion'),
94
       64 => array('quadryllion'),
95
      128 => array('quintyllion'),
96
      256 => array('sextyllion'),
97
      512 => array('septyllion'),
98
     1024 => array('octyllion'),
99
     2048 => array('nonyllion'),
100
     4096 => array('decyllion'),
101
     8192 => array('undecyllion'),
102
    16384 => array('duodecyllion'),
103
    32768 => array('tredecyllion'),
104
    65536 => array('quattuordecyllion'),
105
   131072 => array('quindecyllion'),
106
   262144 => array('sexdecyllion'),
107
   524288 => array('septendecyllion'),
108
  1048576 => array('octodecyllion'),
109
  2097152 => array('novemdecyllion'),
110
  4194304 => array('vigintyllion')
111
        );
112
 
113
    /**
114
     * The array containing the digits (indexed by the digits themselves).
115
     * @var array
116
     * @access private
117
     */
118
    var $_digits = array(
119
 
120
        'five', 'six', 'seven', 'eight', 'nine'
121
    );
122
 
123
    /**
124
     * The word separator
125
     * @var string
126
     * @access private
127
     */
128
    var $_sep = ' ';
129
    // }}}
130
    // {{{ toWords()
131
 
132
    /**
133
     * Converts a number to its word representation
134
     * in Donald Knuth system, in English language.
135
     *
136
     * @param integer $num       An integer between -infinity and infinity inclusive :)
137
     *                           that need to be converted to words
138
     * @param integer $power     The power of ten for the rest of the number to the right.
139
     *                           Optional, defaults to 0.
140
     * @param integer $powsuffix The power name to be added to the end of the return string.
141
     *                            Used internally. Optional, defaults to ''.
142
     *
143
     * @return string  The corresponding word representation
144
     *
145
     * @access public
146
     * @author Piotr Klaban <makler@man.torun.pl>
147
     * @since  PHP 4.2.3
148
     */
149
    function toWords($num, $power = 0, $powsuffix = '')
150
    {
151
        $ret = '';
152
 
153
        // add a minus sign
154
        if (substr($num, 0, 1) == '-') {
155
            $ret = $this->_sep . $this->_minus;
156
            $num = substr($num, 1);
157
        }
158
 
159
        // strip excessive zero signs and spaces
160
        $num = trim($num);
161
        $num = preg_replace('/^0+/', '', $num);
162
 
163
        if (strlen($num) > 3) {
164
            $maxp = strlen($num)-1;
165
            $curp = $maxp;
166
            for ($p = $maxp; $p > 0; --$p) { // power
167
 
168
                // check for highest power
169
                if (isset($this->_exponent[$p])) {
170
                    // send substr from $curp to $p
171
                    $snum = substr($num, $maxp - $curp, $curp - $p + 1);
172
                    $snum = preg_replace('/^0+/', '', $snum);
173
                    if ($snum !== '') {
174
                        $cursuffix = $this->_exponent[$power][count($this->_exponent[$power])-1];
175
                        if ($powsuffix != '') {
176
                            $cursuffix .= $this->_sep . $powsuffix;
177
                        }
178
 
179
                        $ret .= $this->toWords($snum, $p, $cursuffix);
180
                    }
181
                    $curp = $p - 1;
182
                    continue;
183
                }
184
            }
185
            $num = substr($num, $maxp - $curp, $curp - $p + 1);
186
            if ($num == 0) {
187
                return $ret;
188
            }
189
        } elseif ($num == 0 || $num == '') {
190
            return $this->_sep . $this->_digits[0];
191
        }
192
 
193
        $h = $t = $d = 0;
194
 
195
        switch(strlen($num)) {
196
        case 3:
197
            $h = (int)substr($num, -3, 1);
198
 
199
        case 2:
200
            $t = (int)substr($num, -2, 1);
201
 
202
        case 1:
203
            $d = (int)substr($num, -1, 1);
204
            break;
205
 
206
        case 0:
207
            return;
208
            break;
209
        }
210
 
211
        if ($h) {
212
            $ret .= $this->_sep . $this->_digits[$h] . $this->_sep . 'hundred';
213
 
214
            // in English only - add ' and' for [1-9]01..[1-9]99
215
            // (also for 1001..1099, 10001..10099 but it is harder)
216
            // for now it is switched off, maybe some language purists
217
            // can force me to enable it, or to remove it completely
218
            // if (($t + $d) > 0)
219
            //   $ret .= $this->_sep . 'and';
220
        }
221
 
222
        // ten, twenty etc.
223
        switch ($t) {
224
        case 9:
225
        case 7:
226
        case 6:
227
            $ret .= $this->_sep . $this->_digits[$t] . 'ty';
228
            break;
229
 
230
        case 8:
231
            $ret .= $this->_sep . 'eighty';
232
            break;
233
 
234
        case 5:
235
            $ret .= $this->_sep . 'fifty';
236
            break;
237
 
238
        case 4:
239
            $ret .= $this->_sep . 'forty';
240
            break;
241
 
242
        case 3:
243
            $ret .= $this->_sep . 'thirty';
244
            break;
245
 
246
        case 2:
247
            $ret .= $this->_sep . 'twenty';
248
            break;
249
 
250
        case 1:
251
            switch ($d) {
252
            case 0:
253
                $ret .= $this->_sep . 'ten';
254
                break;
255
 
256
            case 1:
257
                $ret .= $this->_sep . 'eleven';
258
                break;
259
 
260
            case 2:
261
                $ret .= $this->_sep . 'twelve';
262
                break;
263
 
264
            case 3:
265
                $ret .= $this->_sep . 'thirteen';
266
                break;
267
 
268
            case 4:
269
            case 6:
270
            case 7:
271
            case 9:
272
                $ret .= $this->_sep . $this->_digits[$d] . 'teen';
273
                break;
274
 
275
            case 5:
276
                $ret .= $this->_sep . 'fifteen';
277
                break;
278
 
279
            case 8:
280
                $ret .= $this->_sep . 'eighteen';
281
                break;
282
            }
283
            break;
284
        }
285
 
286
        if ($t != 1 && $d > 0) { // add digits only in <0>,<1,9> and <21,inf>
287
            // add minus sign between [2-9] and digit
288
            if ($t > 1) {
289
                $ret .= '-' . $this->_digits[$d];
290
            } else {
291
                $ret .= $this->_sep . $this->_digits[$d];
292
            }
293
        }
294
 
295
        if ($power > 0) {
296
            if (isset($this->_exponent[$power])) {
297
                $lev = $this->_exponent[$power];
298
            }
299
 
300
            if (!isset($lev) || !is_array($lev)) {
301
                return null;
302
            }
303
 
304
            $ret .= $this->_sep . $lev[0];
305
        }
306
 
307
        if ($powsuffix != '') {
308
            $ret .= $this->_sep . $powsuffix;
309
        }
310
 
311
        return $ret;
312
    }
313
    // }}}
314
}
315
 
316
?>