15575c356138ce4e6950c18d0be503a0c3058723
[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 use strict;
36
37 sub init {
38   my $self = shift;
39
40   %{ $self->{numbername} } = (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 sub num2text {
77   my ($self, $amount) = @_;
78
79   return $self->{numbername}{0} unless $amount;
80
81   my @textnumber = ();
82
83   # split amount into chunks of 3
84   my @num = reverse split //, $amount;
85   my @numblock = ();
86   my @a;
87   my $i;
88
89   while (@num) {
90     @a = ();
91     for (1..3) {
92       push(@a, shift(@num));
93     }
94     push(@numblock, join(" ", reverse @a));
95   }
96
97   while (@numblock) {
98
99     $i   = $#numblock;
100     @num = split(//, $numblock[$i]);
101
102     if ($numblock[$i] == 0) {
103       pop @numblock;
104       next;
105     }
106
107     if ($numblock[$i] > 99) {
108
109       # the one from hundreds
110       push(@textnumber, $self->{numbername}{ $num[0] });
111
112       # add hundred designation
113       push(@textnumber, $self->{numbername}{ 10**2 });
114
115       # reduce numblock
116       $numblock[$i] -= $num[0] * 100;
117
118     }
119
120     $numblock[$i] *= 1;
121
122     if ($numblock[$i] > 9) {
123
124       # tens
125       push(@textnumber, $self->format_ten($numblock[$i]));
126     } elsif ($numblock[$i] > 0) {
127
128       # ones
129       push(@textnumber, $self->{numbername}{ $numblock[$i] });
130     }
131
132     # add thousand, million
133     if ($i) {
134       my $num = 10**($i * 3);
135       push(@textnumber, $self->{numbername}{$num});
136     }
137
138     pop(@numblock);
139
140   }
141
142   join(' ', @textnumber);
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 1;
167