This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / sql-ledger / locale / co / 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                   500 => 'quinientos',
70                   700 => 'setecientos',
71                   900 => 'novecientos',
72                 10**2 => 'ciento',
73                 10**3 => 'mil',
74                 10**6 => 'millón',
75                10**12 => 'billón',
76                 );
77
78 }
79
80
81 sub num2text {
82   my ($self, $amount) = @_;
83
84   return $self->{numbername}{0} unless $amount;
85
86   my @textnumber = ();
87
88   # split amount into chunks of 3
89   my @num = reverse split //, $amount;
90   my @numblock = ();
91   my $stripun = 0;
92   my @a = ();
93   my $i;
94
95   while (@num) {
96     @a = ();
97     for (1 .. 3) {
98       push @a, shift @num;
99     }
100     push @numblock, join / /, reverse @a;
101   }
102   
103   # special case for 1000
104   if ($numblock[1] eq '1' && $numblock[0] gt '000') {
105     # remove first array element from textnumber
106     $stripun = 1;
107   }
108
109   while (@numblock) {
110
111     $i = $#numblock;
112     @num = split //, $numblock[$i];
113     
114     $numblock[$i] *= 1;
115
116     if ($numblock[$i] == 0) {
117       pop @numblock;
118       next;
119     }
120     
121     if ($numblock[$i] > 99) {
122       if ($num[0] == 1) {
123         push @textnumber, $self->{numbername}{10**2};
124       } else {
125         # special case for 500, 700, 900
126         if (grep /$num[0]/, (5,7,9)) {
127           push @textnumber, $self->{numbername}{"${num[0]}00"};
128           
129         } else {
130         
131           # the one from hundreds, append cientos
132           push @textnumber, $self->{numbername}{$num[0]}.$self->{numbername}{10**2}.'s';
133           
134         }
135       }
136      
137       # reduce numblock
138       $numblock[$i] -= $num[0] * 100;
139     }
140     
141     if ($numblock[$i] > 9) {
142       # tens
143       push @textnumber, $self->format_ten($numblock[$i], $i);
144     } elsif ($numblock[$i] > 0) {
145       # ones
146       $num = $numblock[$i];
147       $num .= 'o' if ($num == 1 && $i == 0);
148       push @textnumber, $self->{numbername}{$num};
149     }
150     
151     # add thousand, million
152     if ($i) {
153       $num = 10**($i * 3);
154       if ($numblock[$i] > 1) {
155         if ($i == 2 || $i == 4) {
156           $a = $self->{numbername}{$num}."es";
157           $a =~ s/ó/o/;
158           push @textnumber, $a;
159         } elsif ($i == 3) {
160           $num = 10**($i * 2);
161           $a = "$self->{10**3} $self->{numbername}{$num}"."es";
162           $a =~ s/ó/o/;
163           push @textnumber, $a;
164         } else {
165           if ($i == 1) {
166             push @textnumber, $self->{numbername}{$num};
167           } else {
168             push @textnumber, $self->{numbername}{$num}.'s';
169           }
170         }
171       } else {
172         push @textnumber, $self->{numbername}{$num};
173       }
174     }
175       
176     pop @numblock;
177     
178   }
179
180   shift @textnumber if $stripun;
181
182   join ' ', @textnumber;
183
184 }
185
186
187 sub format_ten {
188   my ($self, $amount, $i) = @_;
189   
190   my $textnumber = "";
191   my @num = split //, $amount;
192
193   if ($amount > 30) {
194     $textnumber = $self->{numbername}{$num[0]*10};
195     $amount = $num[1];
196   } else {
197     $amount .= 'o' if ($num[1] == 1 && $i == 0);
198     $textnumber = $self->{numbername}{$amount};
199     $amount = 0;
200   }
201
202   $textnumber .= " y ".$self->{numbername}{$amount} if $amount;
203
204   $textnumber;
205   
206 }
207
208
209 1;
210