This commit was generated by cvs2svn to compensate for changes in r2526,
[freeside.git] / sql-ledger / SL / Num2text.pm
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2001
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Contributors:
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 # this is the default code for the Check package
26 #
27 #=====================================================================
28
29
30 sub init {
31   my $self = shift;
32
33   %{ $self->{numbername} } =
34                    (0 => 'Zero',
35                     1 => 'One',
36                     2 => 'Two',
37                     3 => 'Three',
38                     4 => 'Four',
39                     5 => 'Five',
40                     6 => 'Six',
41                     7 => 'Seven',
42                     8 => 'Eight',
43                     9 => 'Nine',
44                    10 => 'Ten',
45                    11 => 'Eleven',
46                    12 => 'Twelve',
47                    13 => 'Thirteen',
48                    14 => 'Fourteen',
49                    15 => 'Fifteen',
50                    16 => 'Sixteen',
51                    17 => 'Seventeen',
52                    18 => 'Eighteen',
53                    19 => 'Nineteen',
54                    20 => 'Twenty',
55                    30 => 'Thirty',
56                    40 => 'Forty',
57                    50 => 'Fifty',
58                    60 => 'Sixty',
59                    70 => 'Seventy',
60                    80 => 'Eighty',
61                    90 => 'Ninety',
62                 10**2 => 'Hundred',
63                 10**3 => 'Thousand',
64                 10**6 => 'Million',
65                 10**9 => 'Billion',
66                10**12 => 'Trillion',
67                 );
68
69 }
70
71
72 sub num2text {
73   my ($self, $amount) = @_;
74
75   return $self->{numbername}{0} unless $amount;
76
77   my @textnumber = ();
78
79   # split amount into chunks of 3
80   my @num = reverse split //, $amount;
81   my @numblock = ();
82   my @a;
83   my $i;
84
85   while (@num) {
86     @a = ();
87     for (1 .. 3) {
88       push @a, shift @num;
89     }
90     push @numblock, join / /, reverse @a;
91   }
92     
93   while (@numblock) {
94
95     $i = $#numblock;
96     @num = split //, $numblock[$i];
97     
98     if ($numblock[$i] == 0) {
99       pop @numblock;
100       next;
101     }
102    
103     if ($numblock[$i] > 99) {
104       # the one from hundreds
105       push @textnumber, $self->{numbername}{$num[0]};
106      
107       # add hundred designation
108       push @textnumber, $self->{numbername}{10**2};
109
110       # reduce numblock
111       $numblock[$i] -= $num[0] * 100;
112       
113     }
114     
115     $numblock[$i] *= 1;
116     
117     if ($numblock[$i] > 9) {
118       # tens
119       push @textnumber, $self->format_ten($numblock[$i]);
120     } elsif ($numblock[$i] > 0) {
121       # ones
122       push @textnumber, $self->{numbername}{$numblock[$i]};
123     }
124     
125     # add thousand, million
126     if ($i) {
127       $num = 10**($i * 3);
128       push @textnumber, $self->{numbername}{$num};
129     }
130       
131     pop @numblock;
132     
133   }
134
135   join ' ', @textnumber;
136
137 }
138
139
140 sub format_ten {
141   my ($self, $amount) = @_;
142   
143   my $textnumber = "";
144   my @num = split //, $amount;
145
146   if ($amount > 20) {
147     $textnumber = $self->{numbername}{$num[0]*10};
148     $amount = $num[1];
149   } else {
150     $textnumber = $self->{numbername}{$amount};
151     $amount = 0;
152   }
153
154   $textnumber .= " ".$self->{numbername}{$amount} if $amount;
155
156   $textnumber;
157   
158 }
159
160
161 1;
162