Subversion Repositories Applications.annuaire

Rev

Details | Last modification | View Log | RSS feed

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