unstable-Zweig als Kopie des "alten" trunks erstellt.
[kivitendo-erp.git] / SL / Num2text.pm
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #=====================================================================
30 #
31 # this is the default code for the Check package
32 #
33 #=====================================================================
34
35
36 sub init {
37   my $self = shift;
38
39   %{ $self->{numbername} } =
40                    (0 => 'Zero',
41                     1 => 'One',
42                     2 => 'Two',
43                     3 => 'Three',
44                     4 => 'Four',
45                     5 => 'Five',
46                     6 => 'Six',
47                     7 => 'Seven',
48                     8 => 'Eight',
49                     9 => 'Nine',
50                    10 => 'Ten',
51                    11 => 'Eleven',
52                    12 => 'Twelve',
53                    13 => 'Thirteen',
54                    14 => 'Fourteen',
55                    15 => 'Fifteen',
56                    16 => 'Sixteen',
57                    17 => 'Seventeen',
58                    18 => 'Eighteen',
59                    19 => 'Nineteen',
60                    20 => 'Twenty',
61                    30 => 'Thirty',
62                    40 => 'Forty',
63                    50 => 'Fifty',
64                    60 => 'Sixty',
65                    70 => 'Seventy',
66                    80 => 'Eighty',
67                    90 => 'Ninety',
68                 10**2 => 'Hundred',
69                 10**3 => 'Thousand',
70                 10**6 => 'Million',
71                 10**9 => 'Billion',
72                10**12 => 'Trillion',
73                 );
74
75 }
76
77
78 sub num2text {
79   my ($self, $amount) = @_;
80
81   return $self->{numbername}{0} unless $amount;
82
83   my @textnumber = ();
84
85   # split amount into chunks of 3
86   my @num = reverse split //, $amount;
87   my @numblock = ();
88   my @a;
89   my $i;
90
91   while (@num) {
92     @a = ();
93     for (1 .. 3) {
94       push @a, shift @num;
95     }
96     push @numblock, join / /, reverse @a;
97   }
98     
99   while (@numblock) {
100
101     $i = $#numblock;
102     @num = split //, $numblock[$i];
103     
104     if ($numblock[$i] == 0) {
105       pop @numblock;
106       next;
107     }
108    
109     if ($numblock[$i] > 99) {
110       # the one from hundreds
111       push @textnumber, $self->{numbername}{$num[0]};
112      
113       # add hundred designation
114       push @textnumber, $self->{numbername}{10**2};
115
116       # reduce numblock
117       $numblock[$i] -= $num[0] * 100;
118       
119     }
120     
121     $numblock[$i] *= 1;
122     
123     if ($numblock[$i] > 9) {
124       # tens
125       push @textnumber, $self->format_ten($numblock[$i]);
126     } elsif ($numblock[$i] > 0) {
127       # ones
128       push @textnumber, $self->{numbername}{$numblock[$i]};
129     }
130     
131     # add thousand, million
132     if ($i) {
133       $num = 10**($i * 3);
134       push @textnumber, $self->{numbername}{$num};
135     }
136       
137     pop @numblock;
138     
139   }
140
141   join ' ', @textnumber;
142
143 }
144
145
146 sub format_ten {
147   my ($self, $amount) = @_;
148   
149   my $textnumber = "";
150   my @num = split //, $amount;
151
152   if ($amount > 20) {
153     $textnumber = $self->{numbername}{$num[0]*10};
154     $amount = $num[1];
155   } else {
156     $textnumber = $self->{numbername}{$amount};
157     $amount = 0;
158   }
159
160   $textnumber .= " ".$self->{numbername}{$amount} if $amount;
161
162   $textnumber;
163   
164 }
165
166
167 1;
168