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   Filippo Beltramini <phil@esight.it>
20
 * @author   Davide Caironi     <cairo@esight.it>
21
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
22
 * @version  CVS: $Id: lang.it_IT.php 277094 2009-03-13 10:23:23Z quipo $
23
 * @link     http://pear.php.net/package/Numbers_Words
24
 */
25
 
26
/**
27
 * Class for translating numbers into Italian.
28
 *
29
 * @author Filippo Beltramini <phil@esight.it>
30
 * @author Davide Caironi     <cairo@esight.it>
31
 * @package Numbers_Words
32
 */
33
 
34
/**
35
 * Include needed files
36
 */
37
require_once "Numbers/Words.php";
38
 
39
/**
40
 * Class for translating numbers into Italian.
41
 * It supports up to quadrilions
42
 *
43
 * @category Numbers
44
 * @package  Numbers_Words
45
 * @author   Filippo Beltramini <phil@esight.it>
46
 * @author   Davide Caironi     <cairo@esight.it>
47
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
48
 * @link     http://pear.php.net/package/Numbers_Words
49
 */
50
 
51
class Numbers_Words_it_IT extends Numbers_Words
52
{
53
    // {{{ properties
54
 
55
    /**
56
     * Locale name
57
     * @var string
58
     * @access public
59
     */
60
    var $locale = 'it_IT';
61
 
62
    /**
63
     * Language name in English
64
     * @var string
65
     * @access public
66
     */
67
    var $lang = 'Italian';
68
 
69
    /**
70
     * Native language name
71
     * @var string
72
     * @access public
73
     */
74
    var $lang_native = 'Italiano';
75
 
76
    /**
77
     * The word for the minus sign
78
     * @var string
79
     * @access private
80
     */
81
    var $_minus = 'meno ';
82
 
83
    /**
84
     * The sufixes for exponents (singular and plural)
85
     * @var array
86
     * @access private
87
     */
88
    var $_exponent = array(
89
 
90
        3 => array('mille','mila'),
91
        6 => array('milione','miloni'),
92
       12 => array('miliardo','miliardi'),
93
       18 => array('trillone','trilloni'),
94
       24 => array('quadrilione','quadrilioni'),
95
        );
96
    /**
97
     * The array containing the digits (indexed by the digits themselves).
98
     * @var array
99
     * @access private
100
     */
101
    var $_digits = array(
102
 
103
       'cinque', 'sei', 'sette', 'otto', 'nove'
104
    );
105
 
106
    /**
107
     * The word separator
108
     * @var string
109
     * @access private
110
     */
111
    var $_sep = '';
112
    // }}}
113
    // {{{ toWords()
114
    /**
115
     * Converts a number to its word representation
116
     * in italiano.
117
     *
118
     * @param integer $num   An integer between -infinity and infinity inclusive :)
119
     *                        that should be converted to a words representation
120
     * @param integer $power The power of ten for the rest of the number to the right.
121
     *                        For example toWords(12,3) should give "doce mil".
122
     *                        Optional, defaults to 0.
123
     *
124
     * @return string  The corresponding word representation
125
     *
126
     * @access public
127
     * @author Filippo Beltramini
128
     * @since  PHP 4.2.3
129
     */
130
    function toWords($num, $power = 0)
131
    {
132
        // The return string;
133
        $ret = '';
134
 
135
        // add a the word for the minus sign if necessary
136
        if (substr($num, 0, 1) == '-') {
137
            $ret = $this->_sep . $this->_minus;
138
            $num = substr($num, 1);
139
        }
140
 
141
 
142
        // strip excessive zero signs
143
        $num = preg_replace('/^0+/', '', $num);
144
 
145
        if (strlen($num) > 6) {
146
            $current_power = 6;
147
            // check for highest power
148
            if (isset($this->_exponent[$power])) {
149
                // convert the number above the first 6 digits
150
                // with it's corresponding $power.
151
                $snum = substr($num, 0, -6);
152
                $snum = preg_replace('/^0+/', '', $snum);
153
                if ($snum !== '') {
154
                    $ret .= $this->toWords($snum, $power + 6);
155
                }
156
            }
157
            $num = substr($num, -6);
158
            if ($num == 0) {
159
                return $ret;
160
            }
161
        } elseif ($num == 0 || $num == '') {
162
            return(' '.$this->_digits[0].' ');
163
            $current_power = strlen($num);
164
        } else {
165
            $current_power = strlen($num);
166
        }
167
 
168
        // See if we need "thousands"
169
        $thousands = floor($num / 1000);
170
        if ($thousands == 1) {
171
            $ret .= $this->_sep . 'mille' . $this->_sep;
172
        } elseif ($thousands > 1) {
173
            $ret .= $this->toWords($thousands, 3) . $this->_sep;//. 'mil' . $this->_sep;
174
        }
175
 
176
        // values for digits, tens and hundreds
177
        $h = floor(($num / 100) % 10);
178
        $t = floor(($num / 10) % 10);
179
        $d = floor($num % 10);
180
 
181
        // centinaia: duecento, trecento, etc...
182
        switch ($h) {
183
        case 1:
184
            if (($d == 0) and ($t == 0)) { // is it's '100' use 'cien'
185
                $ret .= $this->_sep . 'cento';
186
            } else {
187
                $ret .= $this->_sep . 'cento';
188
            }
189
            break;
190
        case 2:
191
        case 3:
192
        case 4:
193
        case 6:
194
        case 8:
195
            $ret .= $this->_sep . $this->_digits[$h] . 'cento';
196
            break;
197
        case 5:
198
            $ret .= $this->_sep . 'cinquecento';
199
            break;
200
        case 7:
201
            $ret .= $this->_sep . 'settecento';
202
            break;
203
        case 9:
204
            $ret .= $this->_sep . 'novecento';
205
            break;
206
        }
207
 
208
        // decine: venti trenta, etc...
209
        switch ($t) {
210
        case 9:
211
            switch ($d){
212
            case 1:
213
            case 8:
214
                $ret .= $this->_sep . 'novant' ;
215
                break;
216
            default:
217
                $ret .= $this->_sep . 'novanta' ;
218
                break;
219
            }
220
 
221
            break;
222
        case 8:
223
            switch ($d){
224
            case 1:
225
            case 8:
226
                $ret .= $this->_sep . 'ottant' ;
227
                break;
228
            default:
229
                $ret .= $this->_sep . 'ottanta' ;
230
                break;
231
            }
232
 
233
            break;
234
        case 7:
235
            switch ($d){
236
            case 1:
237
            case 8:
238
                $ret .= $this->_sep . 'settant' ;
239
                break;
240
            default:
241
                $ret .= $this->_sep . 'settanta' ;
242
                break;
243
            }
244
            break;
245
        case 6:
246
            switch ($d){
247
            case 1:
248
            case 8:
249
                $ret .= $this->_sep . 'sessant' ;
250
                break;
251
            default:
252
                $ret .= $this->_sep . 'sessanta' ;
253
                break;
254
            }
255
            break;
256
        case 5:
257
            switch ($d){
258
            case 1:
259
            case 8:
260
                $ret .= $this->_sep . 'cinquant' ;
261
                break;
262
            default:
263
                $ret .= $this->_sep . 'cinquanta' ;
264
                break;
265
            }
266
            break;
267
        case 4:
268
            switch ($d){
269
            case 1:
270
            case 8:
271
                $ret .= $this->_sep . 'quarant' ;
272
                break;
273
            default:
274
                $ret .= $this->_sep . 'quaranta' ;
275
                break;
276
            }
277
            break;
278
        case 3:
279
            switch ($d){
280
            case 1:
281
            case 8:
282
                $ret .= $this->_sep . 'trent' ;
283
                break;
284
            default:
285
                $ret .= $this->_sep . 'trenta' ;
286
                break;
287
            }
288
            break;
289
        case 2:
290
            switch ($d){
291
            case 0:
292
                $ret .= $this->_sep . 'venti';
293
                break;
294
            case 1:
295
            case 8:
296
                $ret .= $this->_sep . 'vent' . $this->_digits[$d];
297
                break;
298
            default:
299
                $ret .= $this->_sep . 'venti'  . $this->_digits[$d];
300
                break;
301
            }
302
 
303
 
304
            break;
305
 
306
        case 1:
307
            switch ($d) {
308
            case 0:
309
                $ret .= $this->_sep . 'dieci';
310
                break;
311
 
312
            case 1:
313
                $ret .= $this->_sep . 'undici';
314
                break;
315
 
316
            case 2:
317
                $ret .= $this->_sep . 'dodici';
318
                break;
319
 
320
            case 3:
321
                $ret .= $this->_sep . 'tredici';
322
                break;
323
 
324
            case 4:
325
                $ret .= $this->_sep . 'quattordici';
326
                break;
327
 
328
            case 5:
329
                $ret .= $this->_sep . 'quindici';
330
                break;
331
 
332
            case 6:
333
                 $ret .= $this->_sep . 'sedici';
334
                break;
335
 
336
            case 7:
337
                 $ret .= $this->_sep . 'diciassette';
338
                break;
339
 
340
            case 8:
341
                $ret .= $this->_sep . 'diciotto';
342
                break;
343
 
344
            case 9:
345
                $ret .= $this->_sep . 'diciannove';
346
                break;
347
            }
348
            break;
349
        }
350
 
351
        // add digits only if it is a multiple of 10 and not 1x or 2x
352
        if (($t != 1) and ($t != 2) and ($d > 0)) {
353
             // don't add 'e' for numbers below 10
354
            if ($t != 0) {
355
                // use 'un' instead of 'uno' when there is a suffix ('mila', 'milloni', etc...)
356
                if (($power > 0) and ($d == 1)) {
357
                    $ret .= $this->_sep.' e un';
358
                } else {
359
                    $ret .= $this->_sep.''.$this->_digits[$d];
360
                }
361
            } else {
362
                if (($power > 0) and ($d == 1)) {
363
                    $ret .= $this->_sep.'un ';
364
                } else {
365
                    $ret .= $this->_sep.$this->_digits[$d];
366
                }
367
            }
368
        }
369
 
370
        if ($power > 0) {
371
            if (isset($this->_exponent[$power])) {
372
                $lev = $this->_exponent[$power];
373
            }
374
 
375
            if (!isset($lev) || !is_array($lev)) {
376
                return null;
377
            }
378
 
379
            // if it's only one use the singular suffix
380
            if (($d == 1) and ($t == 0) and ($h == 0)) {
381
                $suffix = $lev[0];
382
            } else {
383
                $suffix = $lev[1];
384
            }
385
            if ($num != 0) {
386
                $ret .= $this->_sep . $suffix;
387
            }
388
        }
389
 
390
        return $ret;
391
    }
392
    // }}}
393
}
394
?>