epic-s6ts
[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., 51 Franklin Street, Fifth Floor, Boston,
29 # MA 02110-1335, USA.
30 #=====================================================================
31 #
32 # this is the default code for the Check package
33 #
34 #=====================================================================
35
36 use strict;
37
38 sub init {
39   my $self = shift;
40
41   %{ $self->{numbername} } = (0      => 'Zero',
42                               1      => 'One',
43                               2      => 'Two',
44                               3      => 'Three',
45                               4      => 'Four',
46                               5      => 'Five',
47                               6      => 'Six',
48                               7      => 'Seven',
49                               8      => 'Eight',
50                               9      => 'Nine',
51                               10     => 'Ten',
52                               11     => 'Eleven',
53                               12     => 'Twelve',
54                               13     => 'Thirteen',
55                               14     => 'Fourteen',
56                               15     => 'Fifteen',
57                               16     => 'Sixteen',
58                               17     => 'Seventeen',
59                               18     => 'Eighteen',
60                               19     => 'Nineteen',
61                               20     => 'Twenty',
62                               30     => 'Thirty',
63                               40     => 'Forty',
64                               50     => 'Fifty',
65                               60     => 'Sixty',
66                               70     => 'Seventy',
67                               80     => 'Eighty',
68                               90     => 'Ninety',
69                               10**2  => 'Hundred',
70                               10**3  => 'Thousand',
71                               10**6  => 'Million',
72                               10**9  => 'Billion',
73                               10**12 => 'Trillion',);
74
75 }
76
77 sub num2text {
78   my ($self, $amount) = @_;
79
80   return $self->{numbername}{0} unless $amount;
81
82   my @textnumber = ();
83
84   # split amount into chunks of 3
85   my @num = reverse split //, $amount;
86   my @numblock = ();
87   my @a;
88   my $i;
89
90   while (@num) {
91     @a = ();
92     for (1..3) {
93       push(@a, shift(@num));
94     }
95     push(@numblock, join(" ", reverse @a));
96   }
97
98   while (@numblock) {
99
100     $i   = $#numblock;
101     @num = split(//, $numblock[$i]);
102
103     if ($numblock[$i] == 0) {
104       pop @numblock;
105       next;
106     }
107
108     if ($numblock[$i] > 99) {
109
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
125       # tens
126       push(@textnumber, $self->format_ten($numblock[$i]));
127     } elsif ($numblock[$i] > 0) {
128
129       # ones
130       push(@textnumber, $self->{numbername}{ $numblock[$i] });
131     }
132
133     # add thousand, million
134     if ($i) {
135       my $num = 10**($i * 3);
136       push(@textnumber, $self->{numbername}{$num});
137     }
138
139     pop(@numblock);
140
141   }
142
143   join(' ', @textnumber);
144
145 }
146
147 sub format_ten {
148   my ($self, $amount) = @_;
149
150   my $textnumber = "";
151   my @num        = split(//, $amount);
152
153   if ($amount > 20) {
154     $textnumber = $self->{numbername}{ $num[0] * 10 };
155     $amount     = $num[1];
156   } else {
157     $textnumber = $self->{numbername}{$amount};
158     $amount     = 0;
159   }
160
161   $textnumber .= " " . $self->{numbername}{$amount} if $amount;
162
163   $textnumber;
164
165 }
166
167 1;
168