This commit was generated by cvs2svn to compensate for changes in r3921,
[freeside.git] / sql-ledger / locale / es_iso / Num2text
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2002
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Language: Spanish
10 #  Contributors: John Christian Stoddart
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #======================================================================
25
26
27 sub init {
28   my $self = shift;
29
30   %{ $self->{numbername} } =
31                    (0 => 'cero',
32                     1 => 'un',
33                  '1o' => 'uno',
34                     2 => 'dos',
35                     3 => 'tres',
36                     4 => 'cuatro',
37                     5 => 'cinco',
38                     6 => 'seis',
39                     7 => 'siete',
40                     8 => 'ocho',
41                     9 => 'nueve',
42                    10 => 'diez',
43                    11 => 'once',
44                    12 => 'doce',
45                    13 => 'trece',
46                    14 => 'catorce',
47                    15 => 'quince',
48                    16 => 'dieciséis',
49                    17 => 'diecisiete',
50                    18 => 'dieciocho',
51                    19 => 'diecinueve',
52                    20 => 'veinte',
53                    21 => 'veintiún',
54                 '21o' => 'veintiuno',
55                    22 => 'veintidós',
56                    23 => 'veintitrés',
57                    24 => 'veinticuatro',
58                    25 => 'veinticinco',
59                    26 => 'veintiséis',
60                    27 => 'veintisiete',
61                    28 => 'veintiocho',
62                    29 => 'veintinueve',
63                    30 => 'treinta',
64                    40 => 'cuarenta',
65                    50 => 'cincuenta',
66                    60 => 'sesenta',
67                    70 => 'setenta',
68                    80 => 'ochenta',
69                    90 => 'noventa',
70                   500 => 'quinientos',
71                   700 => 'setecientos',
72                   900 => 'novecientos',
73                 10**2 => 'ciento',
74                 10**3 => 'mil',
75                 10**6 => 'millón',
76                10**12 => 'billón',
77                 );
78
79 }
80
81
82 sub num2text {
83   my ($self, $amount) = @_;
84
85   return $self->{numbername}{0} unless $amount;
86
87   my @textnumber = ();
88
89   # split amount into chunks of 3
90   my @num = reverse split //, abs($amount);
91   my @numblock = ();
92   my $stripun = 0;
93   my @a = ();
94   my $i;
95
96   while (@num) {
97     @a = ();
98     for (1 .. 3) {
99       push @a, shift @num;
100     }
101     push @numblock, join / /, reverse @a;
102   }
103   
104   # special case for 1000
105   if ($numblock[1] eq '1' && $numblock[0] gt '000') {
106     # remove first array element from textnumber
107     $stripun = 1;
108   }
109
110   while (@numblock) {
111
112     $i = $#numblock;
113     @num = split //, $numblock[$i];
114     
115     $numblock[$i] *= 1;
116
117     if ($numblock[$i] == 0) {
118       pop @numblock;
119       next;
120     }
121     
122     if ($numblock[$i] > 99) {
123       if ($num[0] == 1) {
124         push @textnumber, $self->{numbername}{10**2};
125       } else {
126         # special case for 500, 700, 900
127         if (grep /$num[0]/, (5,7,9)) {
128           push @textnumber, $self->{numbername}{"${num[0]}00"};
129           
130         } else {
131         
132           # the one from hundreds, append cientos
133           push @textnumber, $self->{numbername}{$num[0]}.$self->{numbername}{10**2}.'s';
134           
135         }
136       }
137      
138       # reduce numblock
139       $numblock[$i] -= $num[0] * 100;
140     }
141     
142     if ($numblock[$i] > 9) {
143       # tens
144       push @textnumber, $self->format_ten($numblock[$i], $i);
145     } elsif ($numblock[$i] > 0) {
146       # ones
147       $num = $numblock[$i];
148       $num .= 'o' if ($num == 1 && $i == 0);
149       push @textnumber, $self->{numbername}{$num};
150     }
151     
152     # add thousand, million
153     if ($i) {
154       $num = 10**($i * 3);
155       if ($numblock[$i] > 1) {
156         if ($i == 2 || $i == 4) {
157           $a = $self->{numbername}{$num}."es";
158           $a =~ s/ó/o/;
159           push @textnumber, $a;
160         } elsif ($i == 3) {
161           $num = 10**($i * 2);
162           $a = "$self->{10**3} $self->{numbername}{$num}"."es";
163           $a =~ s/ó/o/;
164           push @textnumber, $a;
165         } else {
166           if ($i == 1) {
167             push @textnumber, $self->{numbername}{$num};
168           } else {
169             push @textnumber, $self->{numbername}{$num}.'s';
170           }
171         }
172       } else {
173         push @textnumber, $self->{numbername}{$num};
174       }
175     }
176       
177     pop @numblock;
178     
179   }
180
181   shift @textnumber if $stripun;
182
183   join ' ', @textnumber;
184
185 }
186
187
188 sub format_ten {
189   my ($self, $amount, $i) = @_;
190   
191   my $textnumber = "";
192   my @num = split //, $amount;
193
194   if ($amount > 30) {
195     $textnumber = $self->{numbername}{$num[0]*10};
196     $amount = $num[1];
197   } else {
198     $amount .= 'o' if ($num[1] == 1 && $i == 0);
199     $textnumber = $self->{numbername}{$amount};
200     $amount = 0;
201   }
202
203   $textnumber .= " y ".$self->{numbername}{$amount} if $amount;
204
205   $textnumber;
206   
207 }
208
209
210 1;
211