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
 * @author   Robin Ericsson <robin.ericsson@profecta.se>
21
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
22
 * @version  CVS: $Id: lang.sv.php 269610 2008-11-24 15:01:07Z clockwerx $
23
 * @link     http://pear.php.net/package/Numbers_Words
24
 */
25
 
26
 
27
/**
28
 *
29
 * Class for translating numbers into Swedish.
30
 * @author Robin Ericsson
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 Swedish.
41
 *
42
 * @category Numbers
43
 * @package  Numbers_Words
44
 * @author   Piotr Klaban <makler@man.torun.pl>
45
 * @author   Robin Ericsson <robin.ericsson@profecta.se>
46
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
47
 * @link     http://pear.php.net/package/Numbers_Words
48
 */
49
class Numbers_Words_sv extends Numbers_Words
50
{
51
 
52
    // {{{ properties
53
 
54
    /**
55
     * Locale name
56
     * @var string
57
     * @access public
58
     */
59
    var $locale = 'sv';
60
 
61
    /**
62
     * Language name in English
63
     * @var string
64
     * @access public
65
     */
66
    var $lang = 'Swedish';
67
 
68
    /**
69
     * Native language name
70
     * @var string
71
     * @access public
72
     */
73
    var $lang_native = 'Svenska';
74
 
75
    /**
76
     * The word for the minus sign
77
     * @var string
78
     * @access private
79
     */
80
    var $_minus = 'Minus'; // minus sign
81
 
82
    /**
83
     * The sufixes for exponents (singular and plural)
84
     * @var array
85
     * @access private
86
     */
87
    var $_exponent = array(
88
 
89
        3 => array('tusen', 'tusen'),
90
        6 => array('miljon','miljoner'),
91
        9 => array('miljard','miljarder'),
92
       12 => array('biljon','biljoner'),
93
       15 => array('biljard','biljarder'),
94
       18 => array('triljon','triljoner'),
95
       21 => array('triljard','triljarder'),
96
       24 => array('kvadriljon','kvadriljoner'),
97
       27 => array('kvadriljard','kvadriljarder'),
98
       30 => array('kvintiljon','kvintiljoner'),
99
       33 => array('kvintiljard','kvintiljarder'),
100
       36 => array('sextiljon','sextiljoner'),
101
       39 => array('sextiljard','sextiljarder'),
102
       42 => array('septiljon','septiljoner'),
103
       45 => array('septiljard','septiljarder'),
104
       48 => array('oktiljon','oktiljoner'),
105
       51 => array('oktiljard','oktiljarder'),
106
       54 => array('noniljon','noniljoner'),
107
       57 => array('noniljard','noniljarder'),
108
       60 => array('dekiljon','dekiljoner'),
109
       63 => array('dekiljard','dekiljarder'),
110
      120 => array('vigintiljon','vigintiljoner'),
111
      123 => array('vigintiljard','vigintiljarder'),
112
      600 => array('centiljon','centiljoner'),
113
      603 => array('centiljard','centiljarder')
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
        'fem', 'sex', 'sju', 'åtta', 'nio'
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 Swedish 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 private
157
     * @author Piotr Klaban <makler@man.torun.pl>
158
     * @author Robin Ericsson <lobbin@localhost.nu>
159
     * @since  PHP 4.2.3
160
     */
161
    function toWords($num, $power = 0, $powsuffix = '')
162
    {
163
        $ret = '';
164
 
165
        // add a minus sign
166
        if (substr($num, 0, 1) == '-') {
167
            $ret = $this->_sep . $this->_minus;
168
            $num = substr($num, 1);
169
        }
170
 
171
        // strip excessive zero signs and spaces
172
        $num = trim($num);
173
        $num = preg_replace('/^0+/', '', $num);
174
 
175
        if (strlen($num) > 3) {
176
            $maxp = strlen($num)-1;
177
            $curp = $maxp;
178
 
179
            for ($p = $maxp; $p > 0; --$p) { // power
180
 
181
                // check for highest power
182
                if (isset($this->_exponent[$p])) {
183
                    // send substr from $curp to $p
184
                    $snum = substr($num, $maxp - $curp, $curp - $p + 1);
185
                    $snum = preg_replace('/^0+/', '', $snum);
186
 
187
                    if ($snum !== '') {
188
                        $cursuffix = $this->_exponent[$power][count($this->_exponent[$power])-1];
189
                        if ($powsuffix != '') {
190
                            $cursuffix .= $this->_sep . $powsuffix;
191
                        }
192
 
193
                        $ret .= $this->toWords($snum, $p, $cursuffix);
194
                    }
195
 
196
                    $curp = $p - 1;
197
                    continue;
198
                }
199
            }
200
 
201
            $num = substr($num, $maxp - $curp, $curp - $p + 1);
202
            if ($num == 0) {
203
                return $ret;
204
            }
205
        } elseif ($num == 0 || $num == '') {
206
            return $this->_sep . $this->_digits[0];
207
        }
208
 
209
        $h = $t = $d = 0;
210
 
211
        switch(strlen($num)) {
212
        case 3:
213
            $h = (int)substr($num, -3, 1);
214
 
215
        case 2:
216
            $t = (int)substr($num, -2, 1);
217
 
218
        case 1:
219
            $d = (int)substr($num, -1, 1);
220
            break;
221
 
222
        case 0:
223
            return;
224
            break;
225
        }
226
 
227
        if ($h) {
228
            $ret .= $this->_sep . $this->_digits[$h] . $this->_sep . 'hundra';
229
        }
230
 
231
        // ten, twenty etc.
232
        switch ($t) {
233
        case 5:
234
        case 6:
235
        case 7:
236
            $ret .= $this->_sep . $this->_digits[$t] . 'tio';
237
            break;
238
 
239
        case 9:
240
            $ret .= $this->_sep . 'nittio';
241
            break;
242
 
243
        case 8:
244
            $ret .= $this->_sep . 'åttio';
245
            break;
246
 
247
        case 4:
248
            $ret .= $this->_sep . 'fyrtio';
249
            break;
250
 
251
        case 3:
252
            $ret .= $this->_sep . 'trettio';
253
            break;
254
 
255
        case 2:
256
            $ret .= $this->_sep . 'tjugo';
257
            break;
258
 
259
        case 1:
260
            switch ($d) {
261
            case 0:
262
                $ret .= $this->_sep . 'tio';
263
                break;
264
 
265
            case 1:
266
                $ret .= $this->_sep . 'elva';
267
                break;
268
 
269
            case 2:
270
                $ret .= $this->_sep . 'tolv';
271
                break;
272
 
273
            case 3:
274
                $ret .= $this->_sep . 'tretton';
275
                break;
276
 
277
            case 4:
278
                $ret .= $this->_sep . 'fjorton';
279
                break;
280
 
281
            case 5:
282
            case 6:
283
                $ret .= $this->_sep . $this->_digits[$d] . 'ton';
284
                break;
285
 
286
            case 7:
287
                $ret .= $this->_sep . 'sjutton';
288
                break;
289
 
290
            case 8:
291
                $ret .= $this->_sep . 'arton';
292
                break;
293
            case 9:
294
                $ret .= $this->_sep . 'nitton';
295
            }
296
            break;
297
        }
298
 
299
        if ($t != 1 && $d > 0) { // add digits only in <0>,<1,9> and <21,inf>
300
            // add minus sign between [2-9] and digit
301
            $ret .= $this->_sep . $this->_digits[$d];
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
            $ret .= $this->_sep . $lev[0];
314
        }
315
 
316
        if ($powsuffix != '') {
317
            $ret .= $this->_sep . $powsuffix;
318
        }
319
 
320
        return $ret;
321
    }
322
    // }}}
323
}
324
 
325
?>