import sql-ledger 2.4.4
[freeside.git] / sql-ledger / old / sql-ledger / locale / es / 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 #  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
26 sub init {
27   my $self = shift;
28
29   %{ $self->{numbername} } =
30                    (0 => 'cero',
31                     1 => 'un',
32                  '1o' => 'uno',
33                     2 => 'dos',
34                     3 => 'tres',
35                     4 => 'cuatro',
36                     5 => 'cinco',
37                     6 => 'seis',
38                     7 => 'siete',
39                     8 => 'ocho',
40                     9 => 'nueve',
41                    10 => 'diez',
42                    11 => 'once',
43                    12 => 'doce',
44                    13 => 'trece',
45                    14 => 'catorce',
46                    15 => 'quince',
47                    16 => 'dieciséis',
48                    17 => 'diecisiete',
49                    18 => 'dieciocho',
50                    19 => 'diecinueve',
51                    20 => 'veinte',
52                    21 => 'veintiún',
53                 '21o' => 'veintiuno',
54                    22 => 'veintidós',
55                    23 => 'veintitrés',
56                    24 => 'veinticuatro',
57                    25 => 'veinticinco',
58                    26 => 'veintiséis',
59                    27 => 'veintisiete',
60                    28 => 'veintiocho',
61                    29 => 'veintinueve',
62                    30 => 'treinta',
63                    40 => 'cuarenta',
64                    50 => 'cincuenta',
65                    60 => 'sesenta',
66                    70 => 'setenta',
67                    80 => 'ochenta',
68                    90 => 'noventa',
69                 10**2 => 'ciento',
70                 10**3 => 'mil',
71                 10**6 => 'millón',
72                 10**9 => 'millardo',
73                10**12 => 'billón',
74                 );
75
76 }
77
78
79 sub num2text {
80   my ($self, $amount) = @_;
81
82   return $self->{numbername}{0} unless $amount;
83
84   my @textnumber = ();
85
86   # split amount into chunks of 3
87   my @num = reverse split //, $amount;
88   my @numblock = ();
89   my $stripun = 0;
90   my @a = ();
91   my $i;
92
93   while (@num) {
94     @a = ();
95     for (1 .. 3) {
96       push @a, shift @num;
97     }
98     push @numblock, join / /, reverse @a;
99   }
100   
101   # special case for 1000
102   if ($numblock[1] eq '1' && $numblock[0] gt '000') {
103     # remove first array element from textnumber
104     $stripun = 1;
105   }
106
107   while (@numblock) {
108
109     $i = $#numblock;
110     @num = split //, $numblock[$i];
111     
112     $numblock[$i] *= 1;
113
114     if ($numblock[$i] == 0) {
115       pop @numblock;
116       next;
117     }
118     
119     if ($numblock[$i] > 99) {
120       if ($num[0] == 1) {
121         push @textnumber, $self->{numbername}{10**2};
122       } else {
123         # the one from hundreds, append cientos
124         push @textnumber, $self->{numbername}{$num[0]}.$self->{numbername}{10**2}.'s';
125       }
126      
127       # reduce numblock
128       $numblock[$i] -= $num[0] * 100;
129     }
130     
131     if ($numblock[$i] > 9) {
132       # tens
133       push @textnumber, $self->format_ten($numblock[$i], $i);
134     } elsif ($numblock[$i] > 0) {
135       # ones
136       $num = $numblock[$i];
137       $num .= 'o' if ($num == 1 && $i == 0);
138       push @textnumber, $self->{numbername}{$num};
139     }
140     
141     # add thousand, million
142     if ($i) {
143       $num = 10**($i * 3);
144       if ($numblock[$i] > 1) {
145         if ($i == 2 || $i == 4) {
146           $a = $self->{numbername}{$num}."es";
147           $a =~ s/ó/o/;
148           push @textnumber, $a;
149         } else {
150           if ($i == 1) {
151             push @textnumber, $self->{numbername}{$num};
152           } else {
153             push @textnumber, $self->{numbername}{$num}.'s';
154           }
155         }
156       } else {
157         push @textnumber, $self->{numbername}{$num};
158       }
159     }
160       
161     pop @numblock;
162     
163   }
164
165   shift @textnumber if $stripun;
166
167   join ' ', @textnumber;
168
169 }
170
171
172 sub format_ten {
173   my ($self, $amount, $i) = @_;
174   
175   my $textnumber = "";
176   my @num = split //, $amount;
177
178   if ($amount > 30) {
179     $textnumber = $self->{numbername}{$num[0]*10};
180     $amount = $num[1];
181   } else {
182     $amount .= 'o' if ($num[1] == 1 && $i == 0);
183     $textnumber = $self->{numbername}{$amount};
184     $amount = 0;
185   }
186
187   $textnumber .= " y ".$self->{numbername}{$amount} if $amount;
188
189   $textnumber;
190   
191 }
192
193
194 1;
195