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   Ernas M. Jamil <ernasm@samba.co.id>
20
 * @author   Arif Rifai Dwiyanto
21
 * @license  PHP 3.0 http://www.php.net/license/3_0.txt
22
 * @version  CVS: $Id: lang.id.php 269650 2008-11-25 03:37:50Z clockwerx $
23
 * @link     http://pear.php.net/package/Numbers_Words
24
 */
25
 
26
require_once "PEAR.php";
27
require_once "Numbers/Words.php";
28
 
29
/**
30
* Class for translating numbers into Indonesian.
31
 *
32
 * @category Numbers
33
 * @package  Numbers_Words
34
 * @author   Ernas M. Jamil <ernasm@samba.co.id>
35
 * @author   Arif Rifai Dwiyanto
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_id extends Numbers_Words
40
{
41
 
42
    // {{{ properties
43
 
44
    /**
45
     * Locale name
46
     * @var string
47
     * @access public
48
     */
49
    var $locale = 'id';
50
 
51
    /**
52
     * Language name in English
53
     * @var string
54
     * @access public
55
     */
56
    var $lang = 'Indonesia Language';
57
 
58
    /**
59
     * Native language name
60
     * @var string
61
     * @access public
62
     */
63
    var $lang_native = 'Bahasa Indonesia';
64
 
65
    /**
66
     * The word for the minus sign
67
     * @var string
68
     * @access private
69
     */
70
    var $_minus = 'minus'; // minus sign
71
 
72
    /**
73
     * The sufixes for exponents (singular and plural)
74
     * Names partly based on:
75
     * http://www.users.dircon.co.uk/~shaunf/shaun/numbers/millions.htm
76
     * @var array
77
     * @access private
78
     */
79
    var $_exponent = array(
80
 
81
        3 => array('ribu'),
82
        6 => array('juta'),
83
        9 => array('milyar'),
84
       12 => array('trilyun'),
85
       24 => array('quadrillion'),
86
       30 => array('quintillion'),
87
       36 => array('sextillion'),
88
       42 => array('septillion'),
89
       48 => array('octillion'),
90
       54 => array('nonillion'),
91
       60 => array('decillion'),
92
       66 => array('undecillion'),
93
       72 => array('duodecillion'),
94
       78 => array('tredecillion'),
95
       84 => array('quattuordecillion'),
96
       90 => array('quindecillion'),
97
       96 => array('sexdecillion'),
98
      102 => array('septendecillion'),
99
      108 => array('octodecillion'),
100
      114 => array('novemdecillion'),
101
      120 => array('vigintillion'),
102
      192 => array('duotrigintillion'),
103
      600 => array('centillion')
104
        );
105
 
106
    /**
107
     * The array containing the digits (indexed by the digits themselves).
108
     * @var array
109
     * @access private
110
     */
111
    var $_digits = array(
112
 
113
        'lima', 'enam', 'tujuh', 'delapan', 'sembilan'
114
    );
115
 
116
    /**
117
     * The word separator
118
     * @var string
119
     * @access private
120
     */
121
    var $_sep = ' ';
122
 
123
    // }}}
124
    // {{{ toWords()
125
 
126
    /**
127
     * Converts a number to its word representation
128
     * in Indonesian language
129
     *
130
     * @param integer $num       An integer between -infinity and infinity inclusive :)
131
     *                           that need to be converted to words
132
     * @param integer $power     The power of ten for the rest of the number to the right.
133
     *                           Optional, defaults to 0.
134
     * @param integer $powsuffix The power name to be added to the end of the return string.
135
     *                            Used internally. Optional, defaults to ''.
136
     *
137
     * @return string  The corresponding word representation
138
     *
139
     * @access public
140
     * @author Ernas M. Jamil
141
     * @since  PHP 4.2.3
142
     */
143
    function toWords($num, $power = 0, $powsuffix = '')
144
    {
145
        $ret = '';
146
 
147
        // add a minus sign
148
        if (substr($num, 0, 1) == '-') {
149
            $ret = $this->_sep . $this->_minus;
150
            $num = substr($num, 1);
151
        }
152
 
153
        // strip excessive zero signs and spaces
154
        $num = trim($num);
155
        $num = preg_replace('/^0+/', '', $num);
156
 
157
        if (strlen($num) > 4) {
158
            $maxp = strlen($num)-1;
159
            $curp = $maxp;
160
            for ($p = $maxp; $p > 0; --$p) { // power
161
 
162
                // check for highest power
163
                if (isset($this->_exponent[$p])) {
164
                    // send substr from $curp to $p
165
                    $snum = substr($num, $maxp - $curp, $curp - $p + 1);
166
                    $snum = preg_replace('/^0+/', '', $snum);
167
                    if ($snum !== '') {
168
                        $cursuffix = $this->_exponent[$power][count($this->_exponent[$power])-1];
169
                        if ($powsuffix != '') {
170
                            $cursuffix .= $this->_sep . $powsuffix;
171
                        }
172
 
173
                        $ret .= $this->toWords($snum, $p, $cursuffix);
174
                    }
175
                    $curp = $p - 1;
176
                    continue;
177
                }
178
            }
179
            $num = substr($num, $maxp - $curp, $curp - $p + 1);
180
            if ($num == 0) {
181
                return $ret;
182
            }
183
        } elseif ($num == 0 || $num == '') {
184
            return $this->_sep . $this->_digits[0];
185
        }
186
 
187
        $h = $t = $d = $th = 0;
188
 
189
        switch (strlen($num)) {
190
        case 4:
191
            $th = (int)substr($num, -4, 1);
192
 
193
        case 3:
194
            $h = (int)substr($num, -3, 1);
195
 
196
        case 2:
197
            $t = (int)substr($num, -2, 1);
198
 
199
        case 1:
200
            $d = (int)substr($num, -1, 1);
201
            break;
202
 
203
        case 0:
204
            return;
205
            break;
206
        }
207
 
208
        if ($th) {
209
            if ($th==1) {
210
                $ret .= $this->_sep . 'seribu';
211
            } else {
212
                $ret .= $this->_sep . $this->_digits[$th] . $this->_sep . 'ribu';
213
            }
214
        }
215
 
216
        if ($h) {
217
            if ($h==1) {
218
                $ret .= $this->_sep . 'seratus';
219
            } else {
220
                $ret .= $this->_sep . $this->_digits[$h] . $this->_sep . 'ratus';
221
            }
222
 
223
            // in English only - add ' and' for [1-9]01..[1-9]99
224
            // (also for 1001..1099, 10001..10099 but it is harder)
225
            // for now it is switched off, maybe some language purists
226
            // can force me to enable it, or to remove it completely
227
            // if (($t + $d) > 0)
228
        }
229
 
230
        // ten, twenty etc.
231
        switch ($t) {
232
        case 9:
233
        case 8:
234
        case 7:
235
        case 6:
236
        case 5:
237
        case 4:
238
        case 3:
239
        case 2:
240
            $ret .= $this->_sep . $this->_digits[$t] . ' puluh';
241
            break;
242
 
243
        case 1:
244
            switch ($d) {
245
            case 0:
246
                $ret .= $this->_sep . 'sepuluh';
247
                break;
248
 
249
            case 1:
250
                $ret .= $this->_sep . 'sebelas';
251
                break;
252
 
253
            case 2:
254
            case 3:
255
            case 4:
256
            case 5:
257
            case 6:
258
            case 7:
259
            case 8:
260
            case 9:
261
                $ret .= $this->_sep . $this->_digits[$d] . ' belas';
262
                break;
263
            }
264
            break;
265
        }
266
 
267
        if ($t != 1 && $d > 0) { // add digits only in <0>,<1,9> and <21,inf>
268
            // add minus sign between [2-9] and digit
269
            if ($t > 1) {
270
                $ret .= ' ' . $this->_digits[$d];
271
            } else {
272
                $ret .= $this->_sep . $this->_digits[$d];
273
            }
274
        }
275
 
276
        if ($power > 0) {
277
            if (isset($this->_exponent[$power])) {
278
                $lev = $this->_exponent[$power];
279
            }
280
 
281
            if (!isset($lev) || !is_array($lev)) {
282
                return null;
283
            }
284
 
285
            $ret .= $this->_sep . $lev[0];
286
        }
287
 
288
        if ($powsuffix != '') {
289
            $ret .= $this->_sep . $powsuffix;
290
        }
291
 
292
        return $ret;
293
    }
294
    // }}}
295
}
296
 
297
?>