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   Jesper Veggerby <pear.nosey@veggerby.dk>
20
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
21
 * @version  CVS: $Id: lang.dk.php 269649 2008-11-25 03:14:15Z clockwerx $
22
 * @link     http://pear.php.net/package/Numbers_Words
23
 */
24
 
25
/**
26
 * Class for translating numbers into Danish.
27
 *
28
 * @author Jesper Veggerby <pear.nosey@veggerby.dk>
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 Danish.
39
 *
40
 * @category Numbers
41
 * @package  Numbers_Words
42
 * @author   Jesper Veggerby <pear.nosey@veggerby.dk>
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_dk extends Numbers_Words
47
{
48
 
49
    // {{{ properties
50
 
51
    /**
52
     * Locale name
53
     * @var string
54
     * @access public
55
     */
56
    var $locale = 'dk';
57
 
58
    /**
59
     * Language name in English
60
     * @var string
61
     * @access public
62
     */
63
    var $lang = 'Danish';
64
 
65
    /**
66
     * Native language name
67
     * @var string
68
     * @access public
69
     */
70
    var $lang_native = 'Dansk';
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
     * From: http://da.wikipedia.org/wiki/Navne_p%E5_store_tal
82
     * @var array
83
     * @access private
84
     */
85
    var $_exponent = array(
86
 
87
        3 => array('tusind','tusinde'),
88
        6 => array('million','millioner'),
89
        9 => array('milliard','milliarder'),
90
       12 => array('billion','billioner'),
91
       15 => array('billiard','billiarder'),
92
       18 => array('trillion','trillioner'),
93
       21 => array('trilliard','trilliarder'),
94
       24 => array('quadrillion','quadrillioner'),
95
       30 => array('quintillion','quintillioner'),
96
       36 => array('sextillion','sextillioner'),
97
       42 => array('septillion','septillioner'),
98
       48 => array('octillion','octillioner'),
99
       54 => array('nonillion','nonillioner'),
100
       60 => array('decillion','decillioner'),
101
      120 => array('vigintillion','vigintillioner'),
102
      600 => array('centillion','centillioner')
103
        );
104
 
105
    /**
106
     * The array containing the digits (indexed by the digits themselves).
107
     * @var array
108
     * @access private
109
     */
110
    var $_digits = array(
111
 
112
        'fem', 'seks', 'syv', 'otte', 'ni'
113
    );
114
 
115
    /**
116
     * The word separator
117
     * @var string
118
     * @access private
119
     */
120
    var $_sep = ' ';
121
 
122
    /**
123
     * The currency names (based on the below links,
124
     * informations from central bank websites and on encyclopedias)
125
     *
126
     * @var array
127
     * @link http://da.wikipedia.org/wiki/Valuta
128
     * @access private
129
     */
130
    var $_currency_names = array(
131
      'AUD' => array(array('australsk dollar', 'australske dollars'), array('cent')),
132
      'CAD' => array(array('canadisk dollar', 'canadisk dollars'), array('cent')),
133
      'CHF' => array(array('schweitzer franc'), array('rappen')),
134
      'CYP' => array(array('cypriotisk pund', 'cypriotiske pund'), array('cent')),
135
      'CZK' => array(array('tjekkisk koruna'), array('halerz')),
136
      'DKK' => array(array('krone', 'kroner'), array('øre')),
137
      'EUR' => array(array('euro'), array('euro-cent')),
138
      'GBP' => array(array('pund'), array('pence')),
139
      'HKD' => array(array('Hong Kong dollar', 'Hong Kong dollars'), array('cent')),
140
      'JPY' => array(array('yen'), array('sen')),
141
      'NOK' => array(array('norsk krone', 'norske kroner'), array('øre')),
142
      'PLN' => array(array('zloty', 'zlotys'), array('grosz')),
143
      'SEK' => array(array('svensk krone', 'svenske kroner'), array('øre')),
144
      'USD' => array(array('dollar', 'dollars'), array('cent'))
145
    );
146
 
147
    /**
148
     * The default currency name
149
     * @var string
150
     * @access public
151
     */
152
    var $def_currency = 'DKK'; // Danish krone
153
 
154
    // }}}
155
    // {{{ toWords()
156
 
157
    /**
158
     * Converts a number to its word representation
159
     * in Danish language
160
     *
161
     * @param integer $num       An integer between -infinity and infinity inclusive :)
162
     *                           that need to be converted to words
163
     * @param integer $power     The power of ten for the rest of the number to the right.
164
     *                           Optional, defaults to 0.
165
     * @param integer $powsuffix The power name to be added to the end of the return string.
166
     *                            Used internally. Optional, defaults to ''.
167
     *
168
     * @return string  The corresponding word representation
169
     *
170
     * @access public
171
     * @author Jesper Veggerby <pear.nosey@veggerby.dk>
172
     * @since  PHP 4.2.3
173
     */
174
    function toWords($num, $power = 0, $powsuffix = '')
175
    {
176
        $ret = '';
177
 
178
        // add a minus sign
179
        if (substr($num, 0, 1) == '-') {
180
            $ret = $this->_sep . $this->_minus;
181
            $num = substr($num, 1);
182
        }
183
 
184
        // strip excessive zero signs and spaces
185
        $num = trim($num);
186
        $num = preg_replace('/^0+/', '', $num);
187
 
188
        if (strlen($num) > 3) {
189
            $maxp = strlen($num)-1;
190
            $curp = $maxp;
191
            for ($p = $maxp; $p > 0; --$p) { // power
192
 
193
                // check for highest power
194
                if (isset($this->_exponent[$p])) {
195
                    // send substr from $curp to $p
196
                    $snum = substr($num, $maxp - $curp, $curp - $p + 1);
197
                    $snum = preg_replace('/^0+/', '', $snum);
198
                    if ($snum !== '') {
199
                        $cursuffix = $this->_exponent[$power][count($this->_exponent[$power])-1];
200
                        if ($powsuffix != '') {
201
                            $cursuffix .= $this->_sep . $powsuffix;
202
                        }
203
 
204
                        $ret .= $this->toWords($snum, $p, $cursuffix);
205
                    }
206
                    $curp = $p - 1;
207
                    continue;
208
                }
209
            }
210
            $num = substr($num, $maxp - $curp, $curp - $p + 1);
211
            if ($num == 0) {
212
                return $ret;
213
            }
214
        } elseif ($num == 0 || $num == '') {
215
            return $this->_sep . $this->_digits[0];
216
        }
217
 
218
        $h = $t = $d = 0;
219
 
220
        switch(strlen($num)) {
221
        case 3:
222
            $h = (int)substr($num, -3, 1);
223
 
224
        case 2:
225
            $t = (int)substr($num, -2, 1);
226
 
227
        case 1:
228
            $d = (int)substr($num, -1, 1);
229
            break;
230
 
231
        case 0:
232
            return;
233
            break;
234
        }
235
 
236
        if ($h) {
237
            if ($h == 1) {
238
                $ret .= $this->_sep . 'et' . $this->_sep . 'hundrede';
239
            } else {
240
                $ret .= $this->_sep . $this->_digits[$h] . $this->_sep . 'hundrede';
241
            }
242
 
243
            //if (($t + $d) > 0)
244
            //  $ret .= $this->_sep . 'og';
245
        } elseif ((isset($maxp)) && ($maxp > 3)) {
246
            // add 'og' in the case where there are preceding thousands but not hundreds or tens,
247
            // so fx. 80001 becomes 'firs tusinde og en' instead of 'firs tusinde en'
248
            $ret .= $this->_sep . 'og';
249
        }
250
 
251
 
252
        if ($t != 1 && $d > 0) {
253
            $ret .= $this->_sep . (($d == 1 & $power == 3 && $t == 0 && $h == 0) ? "et" : $this->_digits[$d]) . ($t > 1 ? $this->_sep . "og" : "");
254
        }
255
 
256
        // ten, twenty etc.
257
        switch ($t) {
258
        case 9:
259
            $ret .= $this->_sep . 'halvfems';
260
            break;
261
 
262
        case 8:
263
            $ret .= $this->_sep . 'firs';
264
            break;
265
 
266
        case 7:
267
            $ret .= $this->_sep . 'halvfjerds';
268
            break;
269
 
270
        case 6:
271
            $ret .= $this->_sep . 'tres';
272
            break;
273
 
274
        case 5:
275
            $ret .= $this->_sep . 'halvtreds';
276
            break;
277
 
278
        case 4:
279
            $ret .= $this->_sep . 'fyrre';
280
            break;
281
 
282
        case 3:
283
            $ret .= $this->_sep . 'tredive';
284
            break;
285
 
286
        case 2:
287
            $ret .= $this->_sep . 'tyve';
288
            break;
289
 
290
        case 1:
291
            switch ($d) {
292
            case 0:
293
                $ret .= $this->_sep . 'ti';
294
                break;
295
 
296
            case 1:
297
                $ret .= $this->_sep . 'elleve';
298
                break;
299
 
300
            case 2:
301
                $ret .= $this->_sep . 'tolv';
302
                break;
303
 
304
            case 3:
305
                $ret .= $this->_sep . 'tretten';
306
                break;
307
 
308
            case 4:
309
                $ret .= $this->_sep . 'fjorten';
310
                break;
311
 
312
            case 5:
313
                $ret .= $this->_sep . 'femten';
314
                break;
315
 
316
            case 6:
317
                $ret .= $this->_sep . 'seksten';
318
                break;
319
 
320
            case 7:
321
                $ret .= $this->_sep . 'sytten';
322
                break;
323
 
324
            case 8:
325
                $ret .= $this->_sep . 'atten';
326
                break;
327
 
328
            case 9:
329
                $ret .= $this->_sep . 'nitten';
330
                break;
331
 
332
            }
333
            break;
334
        }
335
 
336
        if ($power > 0) {
337
            if (isset($this->_exponent[$power])) {
338
                $lev = $this->_exponent[$power];
339
            }
340
 
341
            if (!isset($lev) || !is_array($lev)) {
342
                return null;
343
            }
344
 
345
            if ($d == 1 && ($t+$h) == 0) {
346
                $ret .= $this->_sep . $lev[0];
347
            } else {
348
                $ret .= $this->_sep . $lev[1];
349
            }
350
        }
351
 
352
        if ($powsuffix != '') {
353
            $ret .= $this->_sep . $powsuffix;
354
        }
355
 
356
        return $ret;
357
    }
358
    // }}}
359
    // {{{ toCurrencyWords()
360
 
361
    /**
362
     * Converts a currency value to its word representation
363
     * (with monetary units) in danish language
364
     *
365
     * @param integer $int_curr         An international currency symbol
366
     *                                  as defined by the ISO 4217 standard (three characters)
367
     * @param integer $decimal          A money total amount without fraction part (e.g. amount of dollars)
368
     * @param integer $fraction         Fractional part of the money amount (e.g. amount of cents)
369
     *                                  Optional. Defaults to false.
370
     * @param integer $convert_fraction Convert fraction to words (left as numeric if set to false).
371
     *                                  Optional. Defaults to true.
372
     *
373
     * @return string  The corresponding word representation for the currency
374
     *
375
     * @access public
376
     * @author Jesper Veggerby <pear.nosey@veggerby.dk>
377
     * @since  Numbers_Words 0.4
378
     */
379
    function toCurrencyWords($int_curr, $decimal, $fraction = false, $convert_fraction = true)
380
    {
381
        $int_curr = strtoupper($int_curr);
382
        if (!isset($this->_currency_names[$int_curr])) {
383
            $int_curr = $this->def_currency;
384
        }
385
        $curr_names = $this->_currency_names[$int_curr];
386
 
387
        if (($decimal != "") and ($decimal != 0)) {
388
            $ret = trim($this->toWords($decimal));
389
            $lev = ($decimal == 1) ? 0 : 1;
390
            if ($lev > 0) {
391
                if (count($curr_names[0]) > 1) {
392
                    $ret .= $this->_sep . $curr_names[0][$lev];
393
                } else {
394
                    $ret .= $this->_sep . $curr_names[0][0];
395
                }
396
            } else {
397
                $ret .= $this->_sep . $curr_names[0][0];
398
            }
399
 
400
            if (($fraction !== false)  and ($fraction != 0)) {
401
                $ret .= $this->_sep . "og";
402
            }
403
        }
404
 
405
        if (($fraction !== false) and ($fraction != 0)) {
406
            if ($convert_fraction) {
407
                $ret .= $this->_sep . trim($this->toWords($fraction));
408
            } else {
409
                $ret .= $this->_sep . $fraction;
410
            }
411
            $lev = ($fraction == 1) ? 0 : 1;
412
            if ($lev > 0) {
413
                if (count($curr_names[1]) > 1) {
414
                    $ret .= $this->_sep . $curr_names[1][$lev];
415
                } else {
416
                    $ret .= $this->_sep . $curr_names[1][0];
417
                }
418
            } else {
419
                $ret .= $this->_sep . $curr_names[1][0];
420
            }
421
        }
422
        return $ret;
423
    }
424
    // }}}
425
 
426
 
427
}
428
 
429
?>