This commit was generated by cvs2svn to compensate for changes in r3921,
[freeside.git] / sql-ledger / locale / ee / Num2text
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2003
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Contributors: Mario R. Pizzolanti <mario@zavood.ee>
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
24 #
25 # written for check and receipt printing
26 # it returns a properly formatted text string
27 # for a number up to 10**9
28
29 sub init {
30   my $self = shift;
31
32   %{ $self->{numbername} } =
33                    (0 => 'null',
34                     1 => 'üks',
35                     2 => 'kaks',
36                     3 => 'kolm',
37                     4 => 'neli',
38                     5 => 'viis',
39                     6 => 'kuus',
40                     7 => 'seitse',
41                     8 => 'kaheksa',
42                     9 => 'üheksa',
43                    10 => 'kümme',
44                 10**2 => 'sada',
45                 10**3 => 'tuhat',
46                 10**6 => 'miljon',
47                 10**9 => 'miljard',
48                10**12 => 'biljon'
49                 );
50
51 }
52
53
54 sub num2text {
55   my ($self, $amount) = @_;
56
57   return $self->{numbername}{0} unless $amount;
58
59   my @textnumber = ();
60
61   # split amount into chunks of 3
62   my @num = reverse split //, abs($amount);
63   my @numblock = ();
64   my ($i, $appendit);
65   my @a = ();
66
67   while (@num) {
68     @a = ();
69     for (1 .. 3) {
70       push @a, shift @num;
71     }
72     push @numblock, join / /, reverse @a;
73   }
74   
75   while (@numblock) {
76
77     $i = $#numblock;
78     @num = split //, $numblock[$i];
79     
80     $numblock[$i] *= 1;
81
82     $appendit = "it";
83     
84     if ($numblock[$i] == 0) {
85       pop @numblock;
86       next;
87     }
88    
89     if ($numblock[$i] > 99) {
90       # the one from hundreds
91       push @textnumber, "$self->{numbername}{$num[0]}$self->{numbername}{10**2}";
92      
93       # reduce numblock
94       $numblock[$i] -= $num[0] * 100;
95     }
96     
97     if ($numblock[$i] > 19) {
98       # 20 - 99
99       push @textnumber, "$self->{numbername}{$num[1]}kümmend";
100       @num = split //, $numblock[$i];
101       push @textnumber, $self->{numbername}{$num[1]} if $num[1] > 0;
102       
103     } elsif ($numblock[$i] > 10) {
104       # 11 - 19
105       push @textnumber, "$self->{numbername}{$num[1]}teist";
106       
107     } elsif ($numblock[$i] > 1) {
108       # ones
109       push @textnumber, $self->{numbername}{$numblock[$i]};
110       
111     } elsif ($numblock[$i] == 1) {
112       push @textnumber, $self->{numbername}{$num[0]};
113       $appendit = "";
114       
115     }
116     
117     # add thousand, million
118     if ($i) {
119       $amount = 10**($i * 3);
120       $appendit = ($i == 1) ? "" : $appendit;
121       push @textnumber, "$self->{numbername}{$amount}$appendit";
122     }
123     
124     pop @numblock;
125     
126   }
127
128   join ' ', @textnumber;
129
130 }
131
132
133 1;
134