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.de.php 269649 2008-11-25 03:14:15Z clockwerx $
22
 * @link     http://pear.php.net/package/Numbers_Words
23
 */
24
 
25
/**
26
 *
27
 * Class for translating numbers into German.
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 German.
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_de extends Numbers_Words
47
{
48
 
49
    // {{{ properties
50
 
51
    /**
52
     * Locale name
53
     * @var string
54
     * @access public
55
     */
56
    var $locale = 'de';
57
 
58
    /**
59
     * Language name in English
60
     * @var string
61
     * @access public
62
     */
63
    var $lang = 'German';
64
 
65
    /**
66
     * Native language name
67
     * @var string
68
     * @access public
69
     */
70
    var $lang_native = 'Deutsch';
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 partly based on:
82
     * http://german.about.com/library/blzahlenaud.htm
83
     * http://www3.osk.3web.ne.jp/~nagatani/common/zahlwort.htm
84
     * @var array
85
     * @access private
86
     */
87
    var $_exponent = array(
88
 
89
        3 => array('tausend','tausend'),
90
        6 => array('Million','Millionen'),
91
        9 => array('Milliarde','Milliarden'),
92
       12 => array('Billion','Billionen'),
93
       15 => array('Billiarde','Billiarden'),
94
       18 => array('Trillion','Trillionen'),
95
       21 => array('Trilliarde','Trilliarden'),
96
       24 => array('Quadrillion','Quadrillionen'),
97
       27 => array('Quadrilliarde','Quadrilliarden'),
98
       30 => array('Quintillion','Quintillionen'),
99
       33 => array('Quintilliarde','Quintilliarden'),
100
       36 => array('Sextillion','Sextillionen'),
101
       39 => array('Sextilliarde','Sextilliarden'),
102
       42 => array('Septillion','Septillionen'),
103
       45 => array('Septilliarde','Septilliarden'),
104
       48 => array('Oktillion','Oktillionen'), // oder Octillionen
105
       51 => array('Oktilliarde','Oktilliarden'),
106
       54 => array('Nonillion','Nonillionen'),
107
       57 => array('Nonilliarde','Nonilliarden'),
108
       60 => array('Dezillion','Dezillionen'),
109
       63 => array('Dezilliarde','Dezilliarden'),
110
      120 => array('Vigintillion','Vigintillionen'),
111
      123 => array('Vigintilliarde','Vigintilliarden'),
112
      600 => array('Zentillion','Zentillionen'), // oder Centillion
113
      603 => array('Zentilliarde','Zentilliarden')
114
        );
115
 
116
    /**
117
     * The array containing the digits (indexed by the digits themselves).
118
     * @var array
119
     * @access private
120
     */
121
    var $_digits = array(
122
 
123
        'fünf', 'sechs', 'sieben', 'acht', 'neun'
124
    );
125
 
126
    /**
127
     * The word separator
128
     * @var string
129
     * @access private
130
     */
131
    var $_sep = '';
132
 
133
    /**
134
     * The exponent word separator
135
     * @var string
136
     * @access private
137
     */
138
    var $_sep2 = ' ';
139
 
140
    // }}}
141
    // {{{ toWords()
142
 
143
    /**
144
     * Converts a number to its word representation
145
     * in German language.
146
     *
147
     * @param integer $num       An integer between -infinity and infinity inclusive :)
148
     *                           that need to be converted to words
149
     * @param integer $power     The power of ten for the rest of the number to the right.
150
     *                           Optional, defaults to 0.
151
     * @param integer $powsuffix The power name to be added to the end of the return string.
152
     *                            Used internally. Optional, defaults to ''.
153
     *
154
     * @return string  The corresponding word representation
155
     *
156
     * @access public
157
     * @author Piotr Klaban <makler@man.torun.pl>
158
     * @since  PHP 4.2.3
159
     */
160
    function toWords($num, $power = 0, $powsuffix = '')
161
    {
162
        $ret = '';
163
 
164
        // add a minus sign
165
        if (substr($num, 0, 1) == '-') {
166
            $ret = $this->_sep . $this->_minus;
167
            $num = substr($num, 1);
168
        }
169
 
170
        // strip excessive zero signs and spaces
171
        $num = trim($num);
172
        $num = preg_replace('/^0+/', '', $num);
173
 
174
        if (strlen($num) > 3) {
175
            $maxp = strlen($num)-1;
176
            $curp = $maxp;
177
            for ($p = $maxp; $p > 0; --$p) { // power
178
 
179
                // check for highest power
180
                if (isset($this->_exponent[$p])) {
181
                    // send substr from $curp to $p
182
                    $snum = substr($num, $maxp - $curp, $curp - $p + 1);
183
                    $snum = preg_replace('/^0+/', '', $snum);
184
                    if ($snum !== '') {
185
                        $cursuffix = $this->_exponent[$power][count($this->_exponent[$power])-1];
186
                        if ($powsuffix != '') {
187
                            $cursuffix .= $this->_sep . $powsuffix;
188
                        }
189
 
190
                        $ret .= $this->toWords($snum, $p, $cursuffix);
191
                    }
192
                    $curp = $p - 1;
193
                    continue;
194
                }
195
            }
196
            $num = substr($num, $maxp - $curp, $curp - $p + 1);
197
            if ($num == 0) {
198
                return $ret;
199
            }
200
        } elseif ($num == 0 || $num == '') {
201
            return $this->_sep . $this->_digits[0];
202
        }
203
 
204
        $h = $t = $d = 0;
205
 
206
        switch(strlen($num)) {
207
        case 3:
208
            $h = (int)substr($num, -3, 1);
209
 
210
        case 2:
211
            $t = (int)substr($num, -2, 1);
212
 
213
        case 1:
214
            $d = (int)substr($num, -1, 1);
215
            break;
216
 
217
        case 0:
218
            return;
219
            break;
220
        }
221
 
222
        if ($h) {
223
            $ret .= $this->_sep . $this->_digits[$h] . $this->_sep . 'hundert';
224
        }
225
 
226
        if ($t != 1 && $d > 0) { // add digits only in <0>,<1,9> and <21,inf>
227
            if ($t > 0) {
228
                $ret .= $this->_digits[$d] . 'und';
229
            } else {
230
                $ret .= $this->_digits[$d];
231
                if ($d == 1) {
232
                    if ($power == 0) {
233
                        $ret .= 's'; // fuer eins
234
                    } else {
235
                        if ($power != 3) {  // tausend ausnehmen
236
                            $ret .= 'e'; // fuer eine
237
                        }
238
                    }
239
                }
240
            }
241
        }
242
 
243
        // ten, twenty etc.
244
        switch ($t) {
245
        case 9:
246
        case 8:
247
        case 5:
248
            $ret .= $this->_sep . $this->_digits[$t] . 'zig';
249
            break;
250
 
251
        case 7:
252
            $ret .= $this->_sep . 'siebzig';
253
            break;
254
 
255
        case 6:
256
            $ret .= $this->_sep . 'sechzig';
257
            break;
258
 
259
        case 4:
260
            $ret .= $this->_sep . 'vierzig';
261
            break;
262
 
263
        case 3:
264
            $ret .= $this->_sep . 'dreißig';
265
            break;
266
 
267
        case 2:
268
            $ret .= $this->_sep . 'zwanzig';
269
            break;
270
 
271
        case 1:
272
            switch ($d) {
273
            case 0:
274
                $ret .= $this->_sep . 'zehn';
275
                break;
276
 
277
            case 1:
278
                $ret .= $this->_sep . 'elf';
279
                break;
280
 
281
            case 2:
282
                $ret .= $this->_sep . 'zwölf';
283
                break;
284
 
285
            case 3:
286
            case 4:
287
            case 5:
288
            case 8:
289
            case 9:
290
                $ret .= $this->_sep . $this->_digits[$d] . 'zehn';
291
                break;
292
 
293
            case 6:
294
                $ret .= $this->_sep . 'sechzehn';
295
                break;
296
 
297
            case 7:
298
                $ret .= $this->_sep . 'siebzehn';
299
                break;
300
            }
301
            break;
302
        }
303
 
304
        if ($power > 0) {
305
            if (isset($this->_exponent[$power])) {
306
                $lev = $this->_exponent[$power];
307
            }
308
 
309
            if (!isset($lev) || !is_array($lev)) {
310
                return null;
311
            }
312
 
313
            if ($power == 3) {
314
                $ret .= $this->_sep . $lev[0];
315
            } elseif ($d == 1 && ($t+$h) == 0) {
316
                $ret .= $this->_sep2 . $lev[0] . $this->_sep2;
317
            } else {
318
                $ret .= $this->_sep2 . $lev[1] . $this->_sep2;
319
            }
320
        }
321
 
322
        if ($powsuffix != '') {
323
            $ret .= $this->_sep . $powsuffix;
324
        }
325
 
326
        return $ret;
327
    }
328
    // }}}
329
}
330
 
331
?>