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   Xavier Noguer
20
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
21
 * @version  CVS: $Id: lang.es.php 269650 2008-11-25 03:37:50Z clockwerx $
22
 * @link     http://pear.php.net/package/Numbers_Words
23
 */
24
 
25
/**
26
 * Class for translating numbers into Spanish (Castellano).
27
 *
28
 * @author Xavier Noguer
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 Spanish (Castellano).
39
 * It supports up to decallones (10^6).
40
 * It doesn't support spanish tonic accents (acentos).
41
 *
42
 * @category Numbers
43
 * @package  Numbers_Words
44
 * @author   Xavier Noguer
45
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
46
 * @link     http://pear.php.net/package/Numbers_Words
47
 */
48
class Numbers_Words_es extends Numbers_Words
49
{
50
    // {{{ properties
51
 
52
    /**
53
     * Locale name
54
     * @var string
55
     * @access public
56
     */
57
    var $locale = 'es';
58
 
59
    /**
60
     * Language name in English
61
     * @var string
62
     * @access public
63
     */
64
    var $lang = 'Spanish';
65
 
66
    /**
67
     * Native language name
68
     * @var string
69
     * @access public
70
     */
71
    var $lang_native = 'Español';
72
 
73
    /**
74
     * The word for the minus sign
75
     * @var string
76
     * @access private
77
     */
78
    var $_minus = 'menos';
79
 
80
    /**
81
     * The sufixes for exponents (singular and plural)
82
     * @var array
83
     * @access private
84
     */
85
    var $_exponent = array(
86
 
87
        3 => array('mil','mil'),
88
        6 => array('millón','millones'),
89
       12 => array('billón','billones'),
90
       18 => array('trilón','trillones'),
91
       24 => array('cuatrillón','cuatrillones'),
92
       30 => array('quintillón','quintillones'),
93
       36 => array('sextillón','sextillones'),
94
       42 => array('septillón','septillones'),
95
       48 => array('octallón','octallones'),
96
       54 => array('nonallón','nonallones'),
97
       60 => array('decallón','decallones'),
98
        );
99
    /**
100
     * The array containing the digits (indexed by the digits themselves).
101
     * @var array
102
     * @access private
103
     */
104
    var $_digits = array(
105
 
106
        'cinco', 'seis', 'siete', 'ocho', 'nueve'
107
        );
108
    /**
109
     * The word separator
110
     * @var string
111
     * @access private
112
     */
113
    var $_sep = ' ';
114
    // }}}
115
    // {{{ toWords()
116
    /**
117
     * Converts a number to its word representation
118
     * in Spanish (Castellano).
119
     *
120
     * @param integer $num   An integer between -infinity and infinity inclusive :)
121
     *                        that should be converted to a words representation
122
     * @param integer $power The power of ten for the rest of the number to the right.
123
     *                        For example toWords(12,3) should give "doce mil".
124
     *                        Optional, defaults to 0.
125
     *
126
     * @return string  The corresponding word representation
127
     *
128
     * @access private
129
     * @author Xavier Noguer
130
     * @since  PHP 4.2.3
131
     */
132
    function toWords($num, $power = 0)
133
    {
134
        // The return string;
135
        $ret = '';
136
 
137
        // add a the word for the minus sign if necessary
138
        if (substr($num, 0, 1) == '-') {
139
            $ret = $this->_sep . $this->_minus;
140
            $num = substr($num, 1);
141
        }
142
 
143
 
144
        // strip excessive zero signs
145
        $num = preg_replace('/^0+/', '', $num);
146
 
147
        if (strlen($num) > 6) {
148
            $current_power = 6;
149
            // check for highest power
150
            if (isset($this->_exponent[$power])) {
151
                // convert the number above the first 6 digits
152
                // with it's corresponding $power.
153
                $snum = substr($num, 0, -6);
154
                $snum = preg_replace('/^0+/', '', $snum);
155
                if ($snum !== '') {
156
                    $ret .= $this->toWords($snum, $power + 6);
157
                }
158
            }
159
            $num = substr($num, -6);
160
            if ($num == 0) {
161
                return $ret;
162
            }
163
        } elseif ($num == 0 || $num == '') {
164
            return(' '.$this->_digits[0]);
165
            $current_power = strlen($num);
166
        } else {
167
            $current_power = strlen($num);
168
        }
169
 
170
        // See if we need "thousands"
171
        $thousands = floor($num / 1000);
172
        if ($thousands == 1) {
173
            $ret .= $this->_sep . 'mil';
174
        } elseif ($thousands > 1) {
175
            $ret .= $this->toWords($thousands, 3);
176
        }
177
 
178
        // values for digits, tens and hundreds
179
        $h = floor(($num / 100) % 10);
180
        $t = floor(($num / 10) % 10);
181
        $d = floor($num % 10);
182
 
183
        // cientos: doscientos, trescientos, etc...
184
        switch ($h) {
185
        case 1:
186
            if (($d == 0) and ($t == 0)) { // is it's '100' use 'cien'
187
                $ret .= $this->_sep . 'cien';
188
            } else {
189
                $ret .= $this->_sep . 'ciento';
190
            }
191
            break;
192
        case 2:
193
        case 3:
194
        case 4:
195
        case 6:
196
        case 8:
197
            $ret .= $this->_sep . $this->_digits[$h] . 'cientos';
198
            break;
199
        case 5:
200
            $ret .= $this->_sep . 'quinientos';
201
            break;
202
        case 7:
203
            $ret .= $this->_sep . 'setecientos';
204
            break;
205
        case 9:
206
            $ret .= $this->_sep . 'novecientos';
207
            break;
208
        }
209
 
210
        // decenas: veinte, treinta, etc...
211
        switch ($t) {
212
        case 9:
213
            $ret .= $this->_sep . 'noventa';
214
            break;
215
 
216
        case 8:
217
            $ret .= $this->_sep . 'ochenta';
218
            break;
219
 
220
        case 7:
221
            $ret .= $this->_sep . 'setenta';
222
            break;
223
 
224
        case 6:
225
            $ret .= $this->_sep . 'sesenta';
226
            break;
227
 
228
        case 5:
229
            $ret .= $this->_sep . 'cincuenta';
230
            break;
231
 
232
        case 4:
233
            $ret .= $this->_sep . 'cuarenta';
234
            break;
235
 
236
        case 3:
237
            $ret .= $this->_sep . 'treinta';
238
            break;
239
 
240
        case 2:
241
            if ($d == 0) {
242
                $ret .= $this->_sep . 'veinte';
243
            } else {
244
                if (($power > 0) and ($d == 1)) {
245
                    $ret .= $this->_sep . 'veintiún';
246
                } else {
247
                    $ret .= $this->_sep . 'veinti' . $this->_digits[$d];
248
                }
249
            }
250
            break;
251
 
252
        case 1:
253
            switch ($d) {
254
            case 0:
255
                $ret .= $this->_sep . 'diez';
256
                break;
257
 
258
            case 1:
259
                $ret .= $this->_sep . 'once';
260
                break;
261
 
262
            case 2:
263
                $ret .= $this->_sep . 'doce';
264
                break;
265
 
266
            case 3:
267
                $ret .= $this->_sep . 'trece';
268
                break;
269
 
270
            case 4:
271
                $ret .= $this->_sep . 'catorce';
272
                break;
273
 
274
            case 5:
275
                $ret .= $this->_sep . 'quince';
276
                break;
277
 
278
            case 6:
279
            case 7:
280
            case 9:
281
            case 8:
282
                $ret .= $this->_sep . 'dieci' . $this->_digits[$d];
283
                break;
284
            }
285
            break;
286
        }
287
 
288
        // add digits only if it is a multiple of 10 and not 1x or 2x
289
        if (($t != 1) and ($t != 2) and ($d > 0)) {
290
 
291
            // don't add 'y' for numbers below 10
292
            if ($t != 0) {
293
                // use 'un' instead of 'uno' when there is a suffix ('mil', 'millones', etc...)
294
                if (($power > 0) and ($d == 1)) {
295
                    $ret .= $this->_sep.' y un';
296
                } else {
297
                    $ret .= $this->_sep.'y '.$this->_digits[$d];
298
                }
299
            } else {
300
                if (($power > 0) and ($d == 1)) {
301
                    $ret .= $this->_sep.'un';
302
                } else {
303
                    $ret .= $this->_sep.$this->_digits[$d];
304
                }
305
            }
306
        }
307
 
308
        if ($power > 0) {
309
            if (isset($this->_exponent[$power])) {
310
                $lev = $this->_exponent[$power];
311
            }
312
 
313
            if (!isset($lev) || !is_array($lev)) {
314
                return null;
315
            }
316
 
317
            // if it's only one use the singular suffix
318
            if (($d == 1) and ($t == 0) and ($h == 0)) {
319
                $suffix = $lev[0];
320
            } else {
321
                $suffix = $lev[1];
322
            }
323
            if ($num != 0) {
324
                $ret .= $this->_sep . $suffix;
325
            }
326
        }
327
 
328
        return $ret;
329
    }
330
    // }}}
331
}
332
?>