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   Kouber Saparev <kouber@php.net>
20
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
21
 * @version  CVS: $Id: lang.fr.php 269609 2008-11-24 14:53:41Z clockwerx $
22
 * @link     http://pear.php.net/package/Numbers_Words
23
 */
24
 
25
/**
26
 * Include needed files
27
 */
28
require_once "Numbers/Words.php";
29
 
30
/**
31
 * Class for translating numbers into French.
32
 *
33
 * @category Numbers
34
 * @package  Numbers_Words
35
 * @author   Kouber Saparev <kouber@php.net>
36
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
37
 * @link     http://pear.php.net/package/Numbers_Words
38
 */
39
class Numbers_Words_fr extends Numbers_Words
40
{
41
 
42
    // {{{ properties
43
 
44
    /**
45
     * Locale name.
46
     * @var string
47
     * @access public
48
     */
49
    var $locale = 'fr';
50
 
51
    /**
52
     * Language name in English.
53
     * @var string
54
     * @access public
55
     */
56
    var $lang = 'French';
57
 
58
    /**
59
     * Native language name.
60
     * @var string
61
     * @access public
62
     */
63
    var $lang_native = 'Français';
64
 
65
    /**
66
     * The words for some numbers.
67
     * @var string
68
     * @access private
69
     */
70
    var $_misc_numbers = array(
71
        10=>'dix',      // 10
72
            'onze',     // 11
73
            'douze',    // 12
74
            'treize',   // 13
75
            'quatorze', // 14
76
            'quinze',   // 15
77
            'seize',    // 16
78
        20=>'vingt',    // 20
79
        30=>'trente',   // 30
80
        40=>'quarante', // 40
81
        50=>'cinquante',// 50
82
        60=>'soixante', // 60
83
       100=>'cent'      // 100
84
    );
85
 
86
 
87
    /**
88
     * The words for digits (except zero).
89
     * @var string
90
     * @access private
91
     */
92
    var $_digits = array(1=>"un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf");
93
 
94
    /**
95
     * The word for zero.
96
     * @var string
97
     * @access private
98
     */
99
    var $_zero = 'zéro';
100
 
101
    /**
102
     * The word for infinity.
103
     * @var string
104
     * @access private
105
     */
106
    var $_infinity = 'infini';
107
 
108
    /**
109
     * The word for the "and" language construct.
110
     * @var string
111
     * @access private
112
     */
113
    var $_and = 'et';
114
 
115
    /**
116
     * The word separator.
117
     * @var string
118
     * @access private
119
     */
120
    var $_sep = ' ';
121
 
122
    /**
123
     * The dash liaison.
124
     * @var string
125
     * @access private
126
     */
127
    var $_dash = '-';
128
 
129
    /**
130
     * The word for the minus sign.
131
     * @var string
132
     * @access private
133
     */
134
    var $_minus = 'moins'; // minus sign
135
 
136
    /**
137
     * The plural suffix (except for hundred).
138
     * @var string
139
     * @access private
140
     */
141
    var $_plural = 's'; // plural suffix
142
 
143
    /**
144
     * The suffixes for exponents (singular).
145
     * @var array
146
     * @access private
147
     */
148
    var $_exponent = array(
149
 
150
        3 => 'mille',
151
        6 => 'million',
152
        9 => 'milliard',
153
       12 => 'trillion',
154
       15 => 'quadrillion',
155
       18 => 'quintillion',
156
       21 => 'sextillion',
157
       24 => 'septillion',
158
       27 => 'octillion',
159
       30 => 'nonillion',
160
       33 => 'decillion',
161
       36 => 'undecillion',
162
       39 => 'duodecillion',
163
       42 => 'tredecillion',
164
       45 => 'quattuordecillion',
165
       48 => 'quindecillion',
166
       51 => 'sexdecillion',
167
       54 => 'septendecillion',
168
       57 => 'octodecillion',
169
       60 => 'novemdecillion',
170
       63 => 'vigintillion',
171
       66 => 'unvigintillion',
172
       69 => 'duovigintillion',
173
       72 => 'trevigintillion',
174
       75 => 'quattuorvigintillion',
175
       78 => 'quinvigintillion',
176
       81 => 'sexvigintillion',
177
       84 => 'septenvigintillion',
178
       87 => 'octovigintillion',
179
       90 => 'novemvigintillion',
180
       93 => 'trigintillion',
181
       96 => 'untrigintillion',
182
       99 => 'duotrigintillion',
183
      102 => 'trestrigintillion',
184
      105 => 'quattuortrigintillion',
185
      108 => 'quintrigintillion',
186
      111 => 'sextrigintillion',
187
      114 => 'septentrigintillion',
188
      117 => 'octotrigintillion',
189
      120 => 'novemtrigintillion',
190
      123 => 'quadragintillion',
191
      126 => 'unquadragintillion',
192
      129 => 'duoquadragintillion',
193
      132 => 'trequadragintillion',
194
      135 => 'quattuorquadragintillion',
195
      138 => 'quinquadragintillion',
196
      141 => 'sexquadragintillion',
197
      144 => 'septenquadragintillion',
198
      147 => 'octoquadragintillion',
199
      150 => 'novemquadragintillion',
200
      153 => 'quinquagintillion',
201
      156 => 'unquinquagintillion',
202
      159 => 'duoquinquagintillion',
203
      162 => 'trequinquagintillion',
204
      165 => 'quattuorquinquagintillion',
205
      168 => 'quinquinquagintillion',
206
      171 => 'sexquinquagintillion',
207
      174 => 'septenquinquagintillion',
208
      177 => 'octoquinquagintillion',
209
      180 => 'novemquinquagintillion',
210
      183 => 'sexagintillion',
211
      186 => 'unsexagintillion',
212
      189 => 'duosexagintillion',
213
      192 => 'tresexagintillion',
214
      195 => 'quattuorsexagintillion',
215
      198 => 'quinsexagintillion',
216
      201 => 'sexsexagintillion',
217
      204 => 'septensexagintillion',
218
      207 => 'octosexagintillion',
219
      210 => 'novemsexagintillion',
220
      213 => 'septuagintillion',
221
      216 => 'unseptuagintillion',
222
      219 => 'duoseptuagintillion',
223
      222 => 'treseptuagintillion',
224
      225 => 'quattuorseptuagintillion',
225
      228 => 'quinseptuagintillion',
226
      231 => 'sexseptuagintillion',
227
      234 => 'septenseptuagintillion',
228
      237 => 'octoseptuagintillion',
229
      240 => 'novemseptuagintillion',
230
      243 => 'octogintillion',
231
      246 => 'unoctogintillion',
232
      249 => 'duooctogintillion',
233
      252 => 'treoctogintillion',
234
      255 => 'quattuoroctogintillion',
235
      258 => 'quinoctogintillion',
236
      261 => 'sexoctogintillion',
237
      264 => 'septoctogintillion',
238
      267 => 'octooctogintillion',
239
      270 => 'novemoctogintillion',
240
      273 => 'nonagintillion',
241
      276 => 'unnonagintillion',
242
      279 => 'duononagintillion',
243
      282 => 'trenonagintillion',
244
      285 => 'quattuornonagintillion',
245
      288 => 'quinnonagintillion',
246
      291 => 'sexnonagintillion',
247
      294 => 'septennonagintillion',
248
      297 => 'octononagintillion',
249
      300 => 'novemnonagintillion',
250
      303 => 'centillion'
251
        );
252
    // }}}
253
 
254
    // {{{ _splitNumber()
255
 
256
    /**
257
     * Split a number to groups of three-digit numbers.
258
     *
259
     * @param mixed $num An integer or its string representation
260
     *                   that need to be split
261
     *
262
     * @return array  Groups of three-digit numbers.
263
     * @access private
264
     * @author Kouber Saparev <kouber@php.net>
265
     * @since  PHP 4.2.3
266
     */
267
    function _splitNumber($num)
268
    {
269
        if (is_string($num)) {
270
            $ret    = array();
271
            $strlen = strlen($num);
272
            $first  = substr($num, 0, $strlen%3);
273
 
274
            preg_match_all('/\d{3}/', substr($num, $strlen%3, $strlen), $m);
275
            $ret =& $m[0];
276
 
277
            if ($first) {
278
                array_unshift($ret, $first);
279
            }
280
 
281
            return $ret;
282
        }
283
        return explode(' ', number_format($num, 0, '', ' ')); // a faster version for integers
284
    }
285
    // }}}
286
 
287
    // {{{ _showDigitsGroup()
288
 
289
    /**
290
     * Converts a three-digit number to its word representation
291
     * in French language.
292
     *
293
     * @param integer $num  An integer between 1 and 999 inclusive.
294
     * @param boolean $last A flag, that determines if it is the last group of digits -
295
     *                      this is used to accord the plural suffix of the "hundreds".
296
     *                      Example: 200 = "deux cents", but 200000 = "deux cent mille".
297
     *
298
     * @return string   The words for the given number.
299
     * @access private
300
     * @author Kouber Saparev <kouber@php.net>
301
     */
302
    function _showDigitsGroup($num, $last = false)
303
    {
304
        $ret = '';
305
 
306
        // extract the value of each digit from the three-digit number
307
        $e = $num%10;                  // ones
308
        $d = ($num-$e)%100/10;         // tens
309
        $s = ($num-$d*10-$e)%1000/100; // hundreds
310
 
311
        // process the "hundreds" digit.
312
        if ($s) {
313
            if ($s>1) {
314
                $ret .= $this->_digits[$s].$this->_sep.$this->_misc_numbers[100];
315
                if ($last && !$e && !$d) {
316
                    $ret .= $this->_plural;
317
                }
318
            } else {
319
                $ret .= $this->_misc_numbers[100];
320
            }
321
            $ret .= $this->_sep;
322
        }
323
 
324
        // process the "tens" digit, and optionally the "ones" digit.
325
        if ($d) {
326
            // in the case of 1, the "ones" digit also must be processed
327
            if ($d==1) {
328
                if ($e<=6) {
329
                    $ret .= $this->_misc_numbers[10+$e];
330
                } else {
331
                    $ret .= $this->_misc_numbers[10].'-'.$this->_digits[$e];
332
                }
333
                $e = 0;
334
            } elseif ($d>5) {
335
                if ($d<8) {
336
                    $ret .= $this->_misc_numbers[60];
337
 
338
                    $resto = $d*10+$e-60;
339
                    if ($e==1) {
340
                        $ret .= $this->_sep.$this->_and.$this->_sep;
341
                    } elseif ($resto) {
342
                        $ret .= $this->_dash;
343
                    }
344
 
345
                    if ($resto) {
346
                        $ret .= $this->_showDigitsGroup($resto);
347
                    }
348
                    $e = 0;
349
                } else {
350
                    $ret .= $this->_digits[4].$this->_dash.$this->_misc_numbers[20];
351
 
352
                    $resto = $d*10+$e-80;
353
                    if ($resto) {
354
                        $ret .= $this->_dash;
355
                        $ret .= $this->_showDigitsGroup($resto);
356
 
357
                        $e = 0;
358
                    } else {
359
                        $ret .= $this->_plural;
360
                    }
361
                }
362
            } else {
363
                $ret .= $this->_misc_numbers[$d*10];
364
            }
365
        }
366
 
367
        // process the "ones" digit
368
        if ($e) {
369
            if ($d) {
370
                if ($e==1) {
371
                    $ret .= $this->_sep.$this->_and.$this->_sep;
372
                } else {
373
                    $ret .= $this->_dash;
374
                }
375
            }
376
            $ret .= $this->_digits[$e];
377
        }
378
 
379
        // strip excessive separators
380
        $ret = rtrim($ret, $this->_sep);
381
 
382
        return $ret;
383
    }
384
    // }}}
385
 
386
    // {{{ toWords()
387
 
388
    /**
389
     * Converts a number to its word representation
390
     * in French language.
391
     *
392
     * @param integer $num An integer (or its string representation) between 9.99*-10^302
393
     *                        and 9.99*10^302 (999 centillions) that need to be converted to words
394
     *
395
     * @return string  The corresponding word representation
396
     * @access public
397
     * @author Kouber Saparev <kouber@php.net>
398
     */
399
    function toWords($num = 0)
400
    {
401
        $ret = '';
402
 
403
        // check if $num is a valid non-zero number
404
        if (!$num || preg_match('/^-?0+$/', $num) || !preg_match('/^-?\d+$/', $num)) {
405
            return $this->_zero;
406
        }
407
 
408
        // add a minus sign
409
        if (substr($num, 0, 1) == '-') {
410
            $ret = $this->_minus . $this->_sep;
411
            $num = substr($num, 1);
412
        }
413
 
414
        // if the absolute value is greater than 9.99*10^302, return infinity
415
        if (strlen($num)>306) {
416
            return $ret . $this->_infinity;
417
        }
418
 
419
        // strip excessive zero signs
420
        $num = ltrim($num, '0');
421
 
422
        // split $num to groups of three-digit numbers
423
        $num_groups = $this->_splitNumber($num);
424
 
425
        $sizeof_numgroups = count($num_groups);
426
 
427
        foreach ($num_groups as $i=>$number) {
428
            // what is the corresponding exponent for the current group
429
            $pow = $sizeof_numgroups-$i;
430
 
431
            // skip processment for empty groups
432
            if ($number!='000') {
433
                if ($number!=1 || $pow!=2) {
434
                    $ret .= $this->_showDigitsGroup($number, $i+1==$sizeof_numgroups).$this->_sep;
435
                }
436
                $ret .= $this->_exponent[($pow-1)*3];
437
                if ($pow>2 && $number>1) {
438
                    $ret .= $this->_plural;
439
                }
440
                $ret .= $this->_sep;
441
            }
442
        }
443
 
444
        return rtrim($ret, $this->_sep);
445
    }
446
    // }}}
447
}
448
?>