Alle Dateien durch Perltidy laufen lassen. Die verwendeten Optionen sind am Ende...
[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 sub init {
36   my $self = shift;
37
38   %{ $self->{numbername} } = (0      => 'Zero',
39                               1      => 'One',
40                               2      => 'Two',
41                               3      => 'Three',
42                               4      => 'Four',
43                               5      => 'Five',
44                               6      => 'Six',
45                               7      => 'Seven',
46                               8      => 'Eight',
47                               9      => 'Nine',
48                               10     => 'Ten',
49                               11     => 'Eleven',
50                               12     => 'Twelve',
51                               13     => 'Thirteen',
52                               14     => 'Fourteen',
53                               15     => 'Fifteen',
54                               16     => 'Sixteen',
55                               17     => 'Seventeen',
56                               18     => 'Eighteen',
57                               19     => 'Nineteen',
58                               20     => 'Twenty',
59                               30     => 'Thirty',
60                               40     => 'Forty',
61                               50     => 'Fifty',
62                               60     => 'Sixty',
63                               70     => 'Seventy',
64                               80     => 'Eighty',
65                               90     => 'Ninety',
66                               10**2  => 'Hundred',
67                               10**3  => 'Thousand',
68                               10**6  => 'Million',
69                               10**9  => 'Billion',
70                               10**12 => 'Trillion',);
71
72 }
73
74 sub num2text {
75   my ($self, $amount) = @_;
76
77   return $self->{numbername}{0} unless $amount;
78
79   my @textnumber = ();
80
81   # split amount into chunks of 3
82   my @num = reverse split //, $amount;
83   my @numblock = ();
84   my @a;
85   my $i;
86
87   while (@num) {
88     @a = ();
89     for (1 .. 3) {
90       push @a, shift @num;
91     }
92     push @numblock, join / /, reverse @a;
93   }
94
95   while (@numblock) {
96
97     $i   = $#numblock;
98     @num = split //, $numblock[$i];
99
100     if ($numblock[$i] == 0) {
101       pop @numblock;
102       next;
103     }
104
105     if ($numblock[$i] > 99) {
106
107       # the one from hundreds
108       push @textnumber, $self->{numbername}{ $num[0] };
109
110       # add hundred designation
111       push @textnumber, $self->{numbername}{ 10**2 };
112
113       # reduce numblock
114       $numblock[$i] -= $num[0] * 100;
115
116     }
117
118     $numblock[$i] *= 1;
119
120     if ($numblock[$i] > 9) {
121
122       # tens
123       push @textnumber, $self->format_ten($numblock[$i]);
124     } elsif ($numblock[$i] > 0) {
125
126       # ones
127       push @textnumber, $self->{numbername}{ $numblock[$i] };
128     }
129
130     # add thousand, million
131     if ($i) {
132       $num = 10**($i * 3);
133       push @textnumber, $self->{numbername}{$num};
134     }
135
136     pop @numblock;
137
138   }
139
140   join ' ', @textnumber;
141
142 }
143
144 sub format_ten {
145   my ($self, $amount) = @_;
146
147   my $textnumber = "";
148   my @num        = split //, $amount;
149
150   if ($amount > 20) {
151     $textnumber = $self->{numbername}{ $num[0] * 10 };
152     $amount     = $num[1];
153   } else {
154     $textnumber = $self->{numbername}{$amount};
155     $amount     = 0;
156   }
157
158   $textnumber .= " " . $self->{numbername}{$amount} if $amount;
159
160   $textnumber;
161
162 }
163
164 1;
165